Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
}
like image 363
Balraj Singh Avatar asked Dec 21 '22 04:12

Balraj Singh


1 Answers

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.

like image 168
harism Avatar answered Apr 17 '23 10:04

harism