创建广播接收器,其显示上安装任何应用程序/卸载应用程序名称和版本号? [英] Created BroadcastReceiver which displays application name and version number on install/ uninstall of any application?

查看:104
本文介绍了创建广播接收器,其显示上安装任何应用程序/卸载应用程序名称和版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建广播接收器,其显示上安装任何应用程序/卸载应用程序名称和版本号。但我正在逐渐通过包名intent.getData()。但是,当我试图使用软件包管理系统来发现应用程序的名称它抛出一个异常,在所有的情况下,安装/卸载/替换。可能是什么可能出现的问题,以及如何这个问题能解决?

code:

 进口android.content.BroadcastReceiver;
    进口android.content.Context;
    进口android.content.Intent;
    进口android.content.pm.ApplicationInfo;
    进口android.content.pm.PackageManager;
    进口android.widget.Toast;公共类ApplicationStatusNotification扩展广播接收器{    / **
     *这个方法接收任何应用程序状态(安装/卸载)和显示细节信息。
     * /
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        //获取应用程序的状态(安装/卸载)
        布尔applicationStatus = intent.getBooleanExtra(Intent.EXTRA_REPLACING,FALSE);
        字符串toastMessage = NULL;        //检查应用程序安装或卸载,并相应地显示消息
        如果(intent.getAction()。等于(android.intent.action.PACKAGE_INSTALL)){
            //应用程序安装
            toastMessage =PACKAGE_INSTALL:+ intent.getData()的toString()+ getApplicationName(上下文,intent.getData()的toString(),PackageManager.GET_UNINSTALLED_PACKAGES。);
        }否则如果(intent.getAction()。等于(android.intent.action.PACKAGE_REMOVED)){
            //应用程序卸载
            toastMessage =PACKAGE_REMOVED:+ intent.getData()的toString()+ getApplicationName;(上下文,intent.getData()的toString(),PackageManager.GET_UNINSTALLED_PACKAGES)。
        }否则如果(intent.getAction()。等于(android.intent.action.PACKAGE_REPLACED)){
            //应用替换
            toastMessage =PACKAGE_REPLACED:+ intent.getData()的toString()+ getApplicationName;(上下文,intent.getData()的toString(),PackageManager.GET_UNINSTALLED_PACKAGES)。
        }        //显示吐司消息
        如果(toastMessage!= NULL){
            Toast.makeText(上下文,toastMessage,Toast.LENGTH_LONG).show();
        }
    }    / **
     *这个方法从应用程序包名获得应用程序的名称命名
     * /
    私人字符串getApplicationName(上下文的背景下,字符串数据,INT标志){        最终的软件包管理系统pckManager = context.getPackageManager();
        ApplicationInfo applicationInformation;
        尝试{
            applicationInformation = pckManager.getApplicationInfo(数据,旗);
        }赶上(PackageManager.NameNotFoundException E){
            applicationInformation = NULL;
        }
        最后弦乐的applicationName =(字符串)(applicationInformation = NULL pckManager.getApplicationLabel(applicationInformation):!?(未知));        返回的applicationName;
    }
}


解决方案

我跟着这个的例子,其中广播接收器介绍如下;

 <接收机器人:名字=PackageChangeReceiver>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.PACKAGE_ADDED/>
        <作用机器人:名字=android.intent.action.PACKAGE_REPLACED/>
        <作用机器人:名字=android.intent.action.PACKAGE_REMOVED/>
        <数据机器人:计划=包/>
    &所述; /意图滤光器>
< /接收器>

现在一次PackageChangeReceiver.onReceive(..)被调用,Intent.getData()URI包含的东西左右; 包:my.test.package 这是由Uri.toString()返回。对于使用软件包管理系统,你应该只提取可通过 Uri.getSchemeSpecificPart()这应该给你 my.test.package检索包名称搜索这ApplicationInfo

此外,基于快速测试,似乎很可能是去除包后有没有可用的ApplicationInfo了。

Created BroadcastReceiver which displays application name and version number on install/ uninstall of any application. But i am getting package name through intent.getData(). But when i am trying to find the name of that application using packagemanager it's throwing an exception in all the cases Install/ Uninstall/ Replaced. What could be the possible problem and how can this be fixed?

CODE:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageManager;
    import android.widget.Toast;

public class ApplicationStatusNotification extends BroadcastReceiver {

    /**
     * This method receives message for any application status(Install/ Uninstall) and display details.
     */
    @Override
    public void onReceive(Context context, Intent intent) {

        // Get application status(Install/ Uninstall)
        boolean applicationStatus = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
        String toastMessage = null;

        // Check if the application is install or uninstall and display the message accordingly
        if(intent.getAction().equals("android.intent.action.PACKAGE_INSTALL")){
            // Application Install
            toastMessage = "PACKAGE_INSTALL: "+  intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
        }else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")){
            // Application Uninstall
            toastMessage = "PACKAGE_REMOVED: "+  intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
        }else if(intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")){
            // Application Replaced
            toastMessage = "PACKAGE_REPLACED: "+  intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
        }

        //Display Toast Message
        if(toastMessage != null){
            Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
        }
    }

    /**
     * This method get application name name from application package name
     */
    private String getApplicationName(Context context, String data, int flag) {

        final PackageManager pckManager = context.getPackageManager();
        ApplicationInfo applicationInformation;
        try {
            applicationInformation = pckManager.getApplicationInfo(data, flag);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInformation = null;
        }
        final String applicationName = (String) (applicationInformation != null ? pckManager.getApplicationLabel(applicationInformation) : "(unknown)");

        return applicationName;
    }
}

解决方案

I followed this example where BroadcastReceiver is introduced as follows;

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

Now once PackageChangeReceiver.onReceive(..) is called, Intent.getData() Uri contains something around; package:my.test.package which is returned by Uri.toString(). For searching this ApplicationInfo using PackageManager you should extract package name only which can be retrieved by Uri.getSchemeSpecificPart() which should give you my.test.package only.

Also, based on quick testing, it seems very likely that after package removal there's no ApplicationInfo available anymore.

这篇关于创建广播接收器,其显示上安装任何应用程序/卸载应用程序名称和版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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