gpt4 book ai didi

android - Intent PACKAGE_ADDED 未注册

转载 作者:太空狗 更新时间:2023-10-29 13:33:48 27 4
gpt4 key购买 nike

你好,我正在尝试检测已安装的应用程序,以便我可以对应用程序进行一些分析,我正在使用我在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com