意图PACKAGE_ADDED不注册 [英] Intent PACKAGE_ADDED not registering
问题描述
您好,我试图检测应用程序安装的,这样我可以做一些分析上的应用程序,我用这个例子,我发现计算器听从我目前的应用程序包安装,但什么也没有发生在我的logcat中。
无效registerReceiver(){
IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme(包);
}
公共无效的onReceive(上下文的背景下,意图意图){
串actionStr = intent.getAction();
如果(Intent.ACTION_PACKAGE_ADDED.equals(actionStr)){
乌里数据= intent.getData();
字符串PKGNAME = data.getEn codedSchemeSpecificPart();
//把手包加...
Log.i(记录服务,包名称);
}
}
<接收器的Android版本:NAME =RealTimeActivity>
<意向滤光器>
<类机器人:名称=android.intent.category.DEFAULT/>
<作用机器人:名称=android.intent.action.PACKAGE_ADDED/>
<作用机器人:名称=android.intent.action.PACKAGE_CHANGED/>
<作用机器人:名称=android.intent.action.PACKAGE_INSTALL/>
<作用机器人:名称=android.intent.action.PACKAGE_REMOVED/>
<作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
&所述; /意图滤光器>
< /接收器>
<使用-权限的Android:名称=android.permission.BROADCAST_PACKAGE_ADDED/>
<使用-权限的Android:名称=android.permission.BROADCAST_PACKAGE_CHANGED/>
<使用-权限的Android:名称=android.permission.BROADCAST_PACKAGE_REMOVED/>
<使用-权限的Android:名称=android.permission.BROADCAST_PACKAGE_INSTALL/>
<使用-权限的Android:名称=android.permission.BROADCAST_PACKAGE_REPLACED/>
由于由于Android 3.1的广播行为的改变,你的应用程序必须在启动前就可以收到应用程序安装/拆卸意图。见kabuko的回答<一href="http://stackoverflow.com/questions/8515310/broadcast-receiver-for-package-added-not-working-from-android-3-1-onwards">in这个线程。
下面的接收器可以接收的是Android 4.0的设备(我在应用程序的活动,首先启动的活动,即应用程序也会启动,则接收器可以接收广播)的我。
&LT;接收机器人:名称=。MyReceiver&GT;
&LT;意向滤光器&gt;
&lt;作用机器人:名称=android.intent.action.PACKAGE_ADDED/&GT;
&lt;作用机器人:名称=android.intent.action.PACKAGE_CHANGED/&GT;
&lt;作用机器人:名称=android.intent.action.PACKAGE_REMOVED/&GT;
&lt;数据机器人:计划=包/&GT;
&所述; /意图滤光器&gt;
&LT; /接收器&GT;
(一些应用程序运行一个虚拟的粘滞服务,以保持应用程序活着,让他们可以得到一定的广播)
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" />
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.
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)
这篇关于意图PACKAGE_ADDED不注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!