[英]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.