Get Activity Details Form ResolveInfo

This example shows how we can get details of the application. The example demonstrates how to get label, icon, name, packageName and className.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it DetailsViaResolveInfo.

2.) Run for output.

Steps:

1.) Create a project named DetailsViaResolveInfo and set the information as stated in the image.

Build Target: Android 4.4
Application Name: DetailsViaResolveInfo
Package Name: com.example.DetailsViaResolveInfo
Activity Name: DetailsViaResolveInfoActivity

detailsresolveinfo1

2.) Open DetailsViaResolveInfoActivity.java file and write following code there:

 

package com.example.detailsviaresolveinfo;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class DetailsViaResolveInfoActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);

setListAdapter(new ArrayAdapter<ResolveInfo>(
this,
android.R.layout.simple_list_item_1,
intentList));

Toast.makeText(getApplicationContext(),
"no of activities: " + intentList.size(),
Toast.LENGTH_LONG).show();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

PackageManager packageManager = getPackageManager();
ResolveInfo resolveInfo = (ResolveInfo)l.getItemAtPosition(position);
ActivityInfo activityInfo = resolveInfo.activityInfo;

AlertDialog.Builder appInfoDialog = new AlertDialog.Builder(DetailsViaResolveInfoActivity.this);

CharSequence label = resolveInfo.loadLabel(packageManager);
appInfoDialog.setTitle(label);

//load icon
ImageView icon = new ImageView(getApplicationContext());
icon.setImageDrawable(resolveInfo.loadIcon(packageManager));
LayoutParams iconLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(iconLayoutParams);

//load package and class name
String name = activityInfo.name;
String packageName = activityInfo.applicationInfo.packageName;
String className = activityInfo.applicationInfo.className;
TextView textName = new TextView(getApplicationContext());
textName.setText("Name: " + name);
TextView textPackageName = new TextView(getApplicationContext());
textPackageName.setText("PackageName: " + packageName);
TextView textClassName = new TextView(getApplicationContext());
textClassName.setText("ClassName: " + className);

LinearLayout dialogLayout = new LinearLayout(getApplicationContext());
LayoutParams dialogLayoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
dialogLayout.setLayoutParams(dialogLayoutParams);

dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.addView(icon);
dialogLayout.addView(textName);
dialogLayout.addView(textPackageName);
dialogLayout.addView(textClassName);

ScrollView scrollView = new ScrollView(getApplicationContext());
LayoutParams scrollViewLayoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(scrollViewLayoutParams);
scrollView.addView(dialogLayout);

appInfoDialog.setView(scrollView);

appInfoDialog.setPositiveButton("OK", null);
appInfoDialog.show();

}
}

 

 

3.) Compile and build the project.

Output

detailsresolveinfo2

detailsresolveinfo3