Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - How to find the package name which has been uninstalled when using Intent.ACTION_PACKAGE_REMOVED

I have an application which is keeping a log of internally developed applications installed on the device. Upon installation a broadcast receiver for Intent.PACKAGE_ADDED is invoked and records the package name using the following code:

public class NewInstallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle b = intent.getExtras();
        int uid = b.getInt(Intent.EXTRA_UID);
        String[] packages = context.getPackageManager().getPackagesForUid(uid);

        ApplicationService appService = new ApplicationService(context);
        appService.ApplicationInstalled(packages);
    }
}

The problem I'm facing is when using a broadcast receiver for Intent.PACKAGE_REMOVED, all reference to the package via the unique Id (UID) comes back with null information (As you would expect, given its already been uninstalled). I have a temporary solution for the meantime, but its not very elegant, and for the next version I would like to have cleaner code. An example of how the code should work:

public class RemoveApplicationReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Bundle b = intent.getExtras();
        int uid = b.getInt(Intent.EXTRA_UID);
        String[] packages = context.getPackageManager().getPackagesForUid(uid);

        ApplicationService appService = new ApplicationService(context);
        appService.ApplicationRemoved(packages);
    }

}

So to recap, the question is:

How, after a program has been removed, can I reference the package name in a broadcast receiver for Intent.PACKAGE_REMOVED.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The package names are in the Intent you got from BroadcasReceiver, use the "getData()" function, there is the ComponentMame of the installed/uninstalled package.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...