Launch Activity By Using ResolveInfo

Last example shows how to get details via resolveInfo, In this example we will explain how we can start activity specified in intent.

Algorithm:

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

2.) Run for output.

Steps:

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

Build Target: Android 4.4
Application Name: ActivityFromResolveInfo
Package Name: com.example.ActivityFromResolveInfo
Activity Name: ActivityFromResolveInfoActivity

activityresolve1

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

 

package com.example.activityfromresolveinfo;

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.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class ActivityFromResolveInfoActivity 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(ActivityFromResolveInfoActivity.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
final String name = activityInfo.name;
final String packageName = activityInfo.applicationInfo.packageName;
final 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.setNegativeButton("Cancel", null);
appInfoDialog.setPositiveButton("Start App",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(packageName, name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

}
});
appInfoDialog.show();

}
}

 

 

3.) Compile and build the project.

Output

activityresolve2

activityresolve3

activityresolve4