2013-03-19 66 views
2

我想添加我的活動(com.myapp.launcher.settings)到一個空的ArrayList。知道它的名字是活動的ResolveInfo對象嗎?

ArrayList<ResolveInfo> selectedApps = new ArrayList<ResolveInfo>(); 
    selectedApps.add(/*WHAT GOES IN HERE?*/); 

但我不知道如何獲得一個ResolveInfo對象與我的活動。


我設法做了一個解決方法,循環遍歷所有應用程序來查找我的活動。但它不是非常有效或實用:

// 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); 
      } 
     } 

回答

6

只需使用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); 
1

事件另一個答案你不知道只有應用程序的packageName

Intent intent = new Intent(); 
intent.setPackage(packageName); 
intent.addCategory(Intent.CATEGORY_LAUNCHER); 
ResolveInfo result = packageManager.resolveActivity(intent, 0); 
相關問題