2013-06-28 39 views
4

我的意圖選擇器是基於白名單(只有選擇的應用程序會出現在意向選擇器中)。代碼是基於另一個代碼的基礎上,反之亦然;列入黑名單的申請。我從here得到的代碼和this是與之相關的相關討論。空的意圖選擇器(沒有應用程序可以執行此操作)

語境選擇器創建方式:

 String[] whitelist = new String[] { "org.schiphol", "nl.negentwee", "org.schipholsecurity", "org.chineseschiphol", "nl.ns", "com.tomtom" }; 

    Intent intent = new Intent(Intent.ACTION_MAIN); 
    //intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    startActivity(generateCustomChooserIntent(intent, whitelist)); 

我用它來創建白名單選擇器的方法;

// Method: 
private Intent generateCustomChooserIntent(Intent prototype, String[] whiteList) { 
    List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>(); 
    Intent chooserIntent; 

    Intent dummy = new Intent(prototype.getAction()); 
    dummy.setType(prototype.getType()); 
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy, 0); 
    MyLog.i(LOG_TAG, "Apps installed on device:" + resInfo.size()); 

    if (!resInfo.isEmpty()) { 
     for (ResolveInfo resolveInfo : resInfo) { 
      // MyLog.i(LOG_TAG, "Looking at:" + resolveInfo.activityInfo.packageName); 

      if (resolveInfo.activityInfo == null) { 
       MyLog.e(LOG_TAG, "resolved application has no activity info, so it is not usable."); 
       continue; 
      } 

      if (Arrays.asList(whiteList).contains(resolveInfo.activityInfo.packageName)) { 
       // MyLog.i(LOG_TAG, "=============================> accepted"); 

       HashMap<String, String> info = new HashMap<String, String>(); 
       info.put("packageName", resolveInfo.activityInfo.packageName); 
       info.put("className", resolveInfo.activityInfo.name); 
       info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager()))); 
       intentMetaInfo.add(info); 
      } else { 
       // MyLog.i(LOG_TAG, "rejected"); 
      } 
     } 

     if (!intentMetaInfo.isEmpty()) { 
      MyLog.i(LOG_TAG, "--- done compiling list ---"); 

      // TODO enable sorting again 
      // sorting for nice readability 
      // Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() { 
      // @Override 
      // public int compare(HashMap<String, String> map, HashMap<String, String> map2) { 
      // return map.get("simpleName").compareTo(map2.get("simpleName")); 
      // } 
      // }); 

      MyLog.i(LOG_TAG, "--- creating custom intent list ---"); 

      // create the custom intent list 
      for (HashMap<String, String> metaInfo : intentMetaInfo) { 
       MyLog.i(LOG_TAG, "adding " + metaInfo.get("packageName") + " to the intent list"); 

       Intent targetedShareIntent = (Intent) prototype.clone(); 
       targetedShareIntent.setPackage(metaInfo.get("packageName")); 
       targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className")); 
       targetedShareIntents.add(targetedShareIntent); 
      } 

      MyLog.i(LOG_TAG, "--- done compiling intent list ---"); 
      MyLog.i(LOG_TAG, "total count targetedShareIntents: " + targetedShareIntents.size()); 

      chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Selecteer reis app (1)"); 
      MyLog.i(LOG_TAG, "--- chooser created ---"); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {})); 

      MyLog.e(LOG_TAG, "returning filled (custom) chooser"); 
      return chooserIntent; 
     } 
    } 

    MyLog.e(LOG_TAG, "returning default chooser (empty)"); 
    return Intent.createChooser(prototype, "Selecteer reis app"); 
} 

現在什麼happends是結果選擇器顯示「沒有應用程序可以執行此操作」雖然logcat中顯示有選擇的5個應用程序。

logcat的已記錄的結果:

06-28 13:04:48.679: I/NavigationTypeActivity(9400): Apps installed on device:356 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling list --- 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- creating custom intent list --- 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.chineseschiphol to the intent list 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schiphol to the intent list 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schipholsecurity to the intent list 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding nl.negentwee to the intent list 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling intent list --- 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): total count targetedShareIntents: 4 
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- chooser created --- 
06-28 13:04:48.687: E/NavigationTypeActivity(9400): returning filled (custom) chooser 
+0

請添加您的解決方案作爲答案,並接受它,以便問題在系統中也顯示爲解決。 – Adinia

回答

1

我接到了一個Android大師告訴我的一個選擇者應當具有的工作意圖來定義,例如在Play商店中或Gmail什麼的一些反饋。從理論上說,您可以提供您自己的應用程序的初始意圖(您確定自己的應用程序已安裝)。

Intent chooserIntent = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=gmail")), "example"); 

然後,這就是爲什麼列表從列表中刪除單個項目(您可能不希望自己的應用程序在選擇器中)。

相關問題