我想制作一个可以在设备上安装或删除其他应用程序时接收广播的应用程序。
我的代码
在 list 中:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>
在 AppListener 中:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AppListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.v(TAG, "there is a broadcast");
}
}
但我无法接收任何广播。我认为这个问题是由于应用权限造成的,有什么想法吗?
感谢您的帮助。
最佳答案
在您的 list 中:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>
在intent-filter标签前添加一行
<data android:scheme="package"/>
所以你的 list 应该是这样的:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
我不确定 PACKAGE_REMOVED Intent 是否真的可用。
关于android - 如何在安装或删除其他应用程序时让我的应用程序接收广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11246326/