Intent PACKAGE_ADDED 未注册

Intent PACKAGE_ADDED not registering(Intent PACKAGE_ADDED 未注册)
本文介绍了Intent PACKAGE_ADDED 未注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

Hello i am trying to detect application installed so that i can do some analysis on the application and i am using this example i found on stackoverflow to listen for package installs from my current application but nothing is happening in my 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 以来广播行为发生变化,您的应用必须先启动,然后才能接收应用安装/删除意图.请参阅 kabuko 的回答 在此线程中.

Due to a broadcast behavior change since Android 3.1, your app must be started before it can receive the app installation/removal intents. See kabuko's answer in this thread.

以下接收器在 Android 4.0 设备上适用于我(我在应用中有一个活动,首先启动活动,即应用也启动,然后接收器可以接收广播).

The following receiver works for me on a Android 4.0 device (I have an activity in the app, first start the activity i.e. the app is also started, then the receiver can receive the broadcast).

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

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

(some apps run a dummy sticky service to keep the app process alive, so that they can receive certain broadcasts)

这篇关于Intent PACKAGE_ADDED 未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)