Created BroadcastReceiver which displays application name and version number on install/ uninstall of any application?

落爺英雄遲暮 提交于 2019-12-23 11:58:36

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/8680874/created-broadcastreceiver-which-displays-application-name-and-version-number-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!