2012-08-09 55 views

回答

1

謝謝,但不是我的本意。我想獲得知道,如果一個特定的接收器在運行的應用程序中聲明運行時,取得這樣說:

private <Receiver extends CyborgReceiver<?>> boolean checkIfBroadcastReceiverIsRegisteredInManifest(Class<Receiver> receiverType) { 
    PackageManager pm = application.getPackageManager(); 
    try { 
     ActivityInfo info = pm.getReceiverInfo(new ComponentName(application, receiverType), PackageManager.GET_RECEIVERS); 
     return info.enabled; 
    } catch (NameNotFoundException e) { 
     return false; 
    } 
} 
+0

CyborgReceiver延伸廣播接收器,以及關於過濾器,我真的不記得的情況。 – TacB0sS 2013-02-10 00:51:53

+0

我認爲這與監視應用程序中的通話事件有關。 – TacB0sS 2013-02-10 00:53:01

3

使用PackageManager,你可以queryBroadcastReceivers()找到誰將回應一個特定的IntentgetInstalledPackages(),你可以找到每個包安裝的接收器。

+0

你打我吧...:d – t0mm13b 2012-08-10 00:36:25

3

的代碼會是這樣的相似,從活動中:

// Query all packages that have the BroadcastReceivers... 
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
PackageManager pm = getPackageManager(); 
final List<ResolveInfo> listPkgs = pm.queryBroadcast(mainIntent, 0); 
if (listPkgs != null && listPkgs.size() > 0){ 
    for(ResolveInfo resInfo : listPkgs){ 
     // Now resInfo will contain the list of packages that has receivers... 
    } 
} 
0

傳入應用程序對象作爲第一個參數,你可以做到這一點與(Application)context.GetApplicationContext()如果你有,那麼你的類,它實現了廣播接收器類作爲第二通論點例如broadcastReceiver.class

public static boolean validateReceiverInManifest(Application application, Class receiverClass) throws PackageManager.NameNotFoundException { 
    PackageManager pm = application.getPackageManager(); 
    String packageName = application.getPackageName(); 

    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_RECEIVERS); 
    ActivityInfo[] receivers = packageInfo.receivers; 
    String receiverClassName = receiverClass.getName(); 
    for (ActivityInfo activityInfo : receivers) { 
     if (activityInfo.name.equals(receiverClassName)) { 
      return true; 
     } 
    } 
    return false; 
}