android - Intent PACKAGE_ADDED 未注册

标签 android android-intent broadcastreceiver package

你好,我正在尝试检测已安装的应用程序,以便我可以对应用程序进行一些分析,我正在使用我在 stackoverflow 上找到的这个示例来监听来 self 当前应用程序的包安装,但我的 logcat 中没有发生任何事情。

void registerReceiver() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");

}

public void onReceive(Context context, Intent intent) {
    String actionStr = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(actionStr)) {
        Uri data = intent.getData();
        String pkgName = data.getEncodedSchemeSpecificPart();
        //handle package adding...
        Log.i("Logging Service", pkgName);

    }

}

<receiver android:name="RealTimeActivity">
    <intent-filter>
   <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.PACKAGE_ADDED"  />
    <action android:name="android.intent.action.PACKAGE_CHANGED" />
    <action android:name="android.intent.action.PACKAGE_INSTALL" />
    <action android:name="android.intent.action.PACKAGE_REMOVED" />
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    </intent-filter>
 </receiver>


<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />

最佳答案

由于自 Android 3.1 起广播行为发生了变化,您的应用必须先启动才能接收应用安装/删除 Intent 。看歌舞伎的回答in this thread .

以下接收器在 Android 4.0 设备上为我工作(我在应用程序中有一个 Activity ,首先启动 Activity 即应用程序也启动,然后接收器可以接收广播)。

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_CHANGED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

(一些应用程序运行虚拟粘性服务来保持应用程序进程处于 Activity 状态,以便它们可以接收某些广播)

关于android - Intent PACKAGE_ADDED 未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834594/

相关文章:

java - 启动 Intent "silently"

Android 服务,可能与否?

android - 我需要在我的 Android 应用程序中实现通知提醒

java - 扩展 BroadcastReceiver 的类不起作用

android - 在 SyncAdapter 中抛出 CursorWindowAllocationException

java - 是否需要在 Swing 的 UI Thread 上更新 UI?

android - 如何通过在android中使用phonegap api将.db文件从 Assets 文件夹复制到数据库文件夹来创建数据库

android - 尝试使用 Retrofit 发出 get 请求时无法为类 java.lang.Object 创建调用适配器

Android Intent 和内存

Android:针对 View 类和 Activity 类的 startActivityForResult & setResult