如何在appwidget中接收Intent.ACTION_PACKAGE_ADDED
和Intent.ACTION_PACKAGE_REMOVED
我尝试在Manifest中添加intent-filter:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- The widget provider -->
<receiver android:name=".NetsWidgetProvider">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="com.oozic.widget.incstage.nets.ACTION_NOT_INSTALL"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<!-- This specifies the widget provider info -->
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetinfo" />
</receiver>
</application>
我也尝试在代码中注册:
@Override
public void onEnabled(Context context) {
registerReceiver(context);
Utils.log(TAG, "Register PACKAGE_ADDED PACKAGE_REMOVED");
}
private void registerReceiver(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
context.getApplicationContext().registerReceiver(this, filter);
}
但两者都没有用。 谢谢!
答案 0 :(得分:8)
在AndroidManifest.xml中添加<data android:scheme="package" />
解决了这个问题。
<receiver android:name=".PackageAddedReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
答案 1 :(得分:3)
<receiver android:name=".NetsWidgetProvider"
android:label="@string/appwidget_name" android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.oozic.widget.incstage.nets.ACTION_NOT_INSTALL"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_DATA_CLEARED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
<!-- This specifies the widget provider info -->
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidgetinfo" />
</receiver>
答案 2 :(得分:1)
我收到PACKAGE_REMOVED / ADDED消息后,在我的自定义启动器中向我的小部件发送广播。这是我发现解决这个问题的唯一工作。