Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android package installer - how to get "Open" and "Done" and activity result both?

My Android Application has an option to upgrade to the newer version, the newer version APK I keep it available under a path in sdcard. On click of Upgrade option I invoke following method.

    public static void launchInstaller(Activity act, String apkPath)
    {
       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       act.startActivityForResult(intent, 0);
    }

The reason I include FLAG_ACTIVITY_NEW_TASK, is because, after upgradation, I want to have "Open" & "Done" options, which aren't shown if I don't use this flag.

When the above code launches the package installer, it has two options OK & Cancel, when user press Cancel, I want to know user cancelled it. But I am unable to know because the onActivityResult is called pre-maturely. I come to a reason why is that happening after reading the following posts.

Android - startActivityForResult immediately triggering onActivityResult

onActivityResult() called prematurely

They ask me to make sure that the Intent I am using to launch the activity doesn't have FLAG_ACTIVITY_NEW_TASK set on it. See here:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

In particular note: "This flag can not be used when the caller is requesting a result from the activity being launched."

If the activity is being launched as part of a new task then Android will immediately call the onActivityResult() with RESULT_CANCELED because an activity in one task can't return results to another task, only activities in the same task can do so.

But in my case, I can't remove FLAG_ACTIVITY_NEW_TASK, because otherwise I will not get "Open" and "Done" options on successful upgradation.

Has anybody faced similar sort of problem? Kindly help me out, as it drives me nuts.

like image 731
Mukesh Bhojwani Avatar asked Sep 25 '13 12:09

Mukesh Bhojwani


1 Answers

You should use package added broadcast intent. Create a broadcast receiver listen package added. If added package equals your package, user confirm install otherwise dismiss install process. If you ask what kind extras can be return by android, android returns EXTRA_UID about package. PackageManager' s method convert uid to package.(getPackagesForUid(int uid) returns String array but it usualy return one item in the array in short usually array length equals 1.

like image 71
nurisezgin Avatar answered Sep 24 '22 14:09

nurisezgin