接受包安装和卸载事件 [英] Receiving package install and uninstall events

查看:102
本文介绍了接受包安装和卸载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想正在安装新的应用程序,但只有当我的应用程序运行时检测。我设法通过一个BroadcastReceiver并激活它AndroidManifest文件中,但是这会如果我的应用程序被关闭,即使检测来检测应用程序的安装。 所以这就是为什么我需要手动激活和取消broadcastreveiver。要做到这一点,我有这个code:

I am trying to detect when a new App is being installed but only if my app is running. I managed to detect the installation of the app by making a BroadcastReceiver and activating it inside the AndroidManifest file but this will detect even if my app is closed. So that is why I need to manually activate and deactivate the broadcastreveiver. To do this I have this code:

br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("Enter", "Enters here");
            Toast.makeText(context, "App Installed!!!!.", Toast.LENGTH_LONG).show();
        }
    };
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
    registerReceiver(br, intentFilter);

这应该举杯安装新的应用程序时。但不幸的是事实并非如此。它不会在的onReceive方法进入。任何帮助是AP preciated。

This should make a toast when a new app is installed. But sadly it does not. It does not enter in the onReceive method. Any help is appreciated.

推荐答案

我试图注册的BroadcastReceiver 在任何清单文件或Java code。但无论这两种方法未能触发的onReceive()方法。 谷歌搜索这个问题后,我发现从SO另一个线程两种方法解决: Android的通知应用

I tried to register the BroadcastReceiver in either manifest file or java code. But both of these two methods failed to trigger the onReceive() method. After googling this problem, I found a solution for both methods from another Thread in SO: Android Notification App

在清单文件:

        <receiver android:name=".YourReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

在Java的code:

In java code:

    IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);

这应该为你工作。

这篇关于接受包安装和卸载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆