2
出於某種原因,我需要多次註冊到相同的接收器,如Manifest中的一次和代碼中的一次。我想聽包更改,即使應用程序沒有運行但已安裝。多次註冊到同一個接收器
1.In所述的AndroidManifest.xml
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
2.Also在代碼我註冊在的構造下面的代碼片斷到廣播接收器。
public PackageChangeReceiver(@ForApplication Context context) {
super();
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
context.registerReceiver(this, filter);
}
但不知何故,Android只收到其中一個間歇性地收到它們兩個。新版本中是否有任何文檔,比如Android操作系統本身的性能改進,例如只接收一次廣播,但多次註冊。它是設備還是操作系統還是版本特定的東西?
Android應用程序只能有一個名稱爲BroadcaseReceiver的應用程序。如果您需要多個接收器,則可以使用不同的名稱創建BroadcastReceivers。 –
實際上,當我放置一個斷點並禁用/啓用默認應用程序時,它會間歇性地工作。我每次收到2次。另外我有一個名字叫BroadcastReceiver。但我正在註冊兩次。 – omersem
我會建議你只註冊一次BroadcastReceiver。如果你需要它動態的,那麼只需從Android Manifest中刪除聲明 –