[英]Android studio: Element receiver duplicated
在我的應用程序中,我想檢測我的包裹何時被替換,因此我有一個以這種方式啟用的接收器:
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="@bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="@bool/is_at_least_api_12" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
將我的項目從 Eclipse 導入 Android Studio 時,出現以下錯誤:
Element receiver#com.x.y.ApplicationsReceiver at AndroidManifest.xml duplicated with element declared at AndroidManifest.xml.
知道如何解決這個問題,因為我需要根據 Android API 級別為不同的意圖過濾器啟用接收器?
出現問題是因為在AndroidManifest
兩次添加相同的類來為不同的 Action 注冊 BroadcastReceiver。
通過在單個BroadcastReceiver
添加多個 Action 來實現:
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="@bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
現在在ApplicationsReceiver
類的onReceive
方法中從 Intent 獲取 Action 並根據 API 級別做想做的事情:
@Override
public void onReceive(Context context,Intent intent) {
String action=intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.MY_PACKAGE_REPLACED")){
// do your code here
}else{
// do your code here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.