1. 程式人生 > >獲取所有APP包名和啟動Activity名

獲取所有APP包名和啟動Activity名

轉載自:http://my.eoe.cn/870420/archive/1314.html

public class MainActivity extends Activity {

	ArrayList<String> list;
	private List<ResolveInfo> mApps;
	private ResolveInfo info;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = (Button) findViewById(R.id.btn);
		ListView listView = (ListView) findViewById(R.id.listView);
		list = new ArrayList<String>();
		getList();
		ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(
				MainActivity.this, android.R.layout.simple_list_item_1, list);
		listView.setAdapter(mAdapter);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				ComponentName comp = new ComponentName(
						"com.android.mmreader362",
						"com.android.mmreader362.mmreader");
				intent.setComponent(comp);
				intent.setAction("android.intent.action.MAIN");
				startActivity(intent);
			}
		});
	}

	private void getList() {
		list.clear();
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
		for (int i = 0; i < mApps.size(); i++) {
			info = mApps.get(i);
			String appLabel = info.loadLabel(getPackageManager()).toString();
			String packagename = info.activityInfo.packageName;
			String appname = info.activityInfo.name;
			list.add("appLabel:" + appLabel + " ||  packagename:" + packagename
					+ " ||  appname:" + appname);
		}
	}

}