android - 如何在安装或删除其他应用程序时让我的应用程序接收广播

标签 android permissions broadcastreceiver

我想制作一个可以在设备上安装或删除其他应用程序时接收广播的应用程序。

我的代码

在 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/

相关文章:

python - Tox 在一个 repo 克隆中运行良好,但在另一个中运行良好

安卓 : save/open picture taken from camera intent

java - 如何删除从移动设备清除应用程序数据后触发的通知

Android studio 运行项目后报错

android - 如何在 android 中播放 youtube 视频?

javascript - 我想使用同一个按钮来播放和暂停。第一次单击我想播放它,第二次单击我想暂停它

android - 从 fragment 关闭应用程序会导致返回到父 Activity

ios - 如何使用照片框架在iOS 9中获取照片访问权限

android - ACTION_BATTERY_CHANGED 疯狂射击

android - 手机锁定时收听音量按钮android