我想添加activity(com.myapp.launcher.settings)到空的ArrayList中:
ArrayList<ResolveInfo> selectedApps = new ArrayList<ResolveInfo>();
selectedApps.add(/*WHAT GOES IN HERE?*/);
但是我不知道怎麼獲取ResolveInfo對象。
我試過使用一個工作環境,將所有的應用循環一遍需找activity,實現之後發現這種方法不是很好用。
// Get an array list of all apps
ArrayList<ResolveInfo> allApps = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for(final ResolveInfo app : allApps) {
// If it's name is "com.myapp.launcher.settings" add it
if(app.activityInfo.name.equals("com.myapp.launcher.settings")) {
selectedApps.add(app);
}
}
使用resolveActivity
配合顯性intent。就可以:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.myapp", "com.myapp.launcher.settings"));
ResolveInfo app = pm.resolveActivity(intent, 0);
selectedApps.add(app);