Install APK programmatically on android
Asked Answered
E

1

7

I've been trying to get an android application to install an APK on the sdcard programmatically but I'm running into a little trouble.

This is how I'm doing it:

Intent intent = new Intent(Intent.ACTION_VIEW);           
intent.setDataAndType("ApkFilePath...","application/vnd.android.package-archive");
activity.startActivityForResult(intent,5000);

Now that works ok, it brings the package manager and I can control what to do when the manager finishes installing the APK.

But the issue that I'm having is that if at the end of the installation the user clicks on "Open" instead of "Done" the "OnActivityResult" method is not called, as the manager still exists.... and this presents another issue on another requirement on the system.

Is there a way to know when the user has selected "Open" at the end of the package manager, or is there a way to force the manager to display only the buttons I want it to display?

Really could use the help, I've search everywhere and don't seem to find a solution

Ezzo answered 15/6, 2011 at 18:25 Comment(3)
Well changed the approach to this, now instead of launching a startActivityForResult I created a broadcast receiver for the application added action and from there I handle the call...works better that wayEzzo
If the solution you found works for you, please post it as an answer (rather than a comment) and accept.Pilch
actually I changed the implementation and did what was suggested in the answer by uwe. In the 'PackageReceiver' I just compared the installed package name with my application package name and then open it up...That way it opens even though the user clicks or not on Open/DoneEzzo
M
9

You can add a receiver to your AndroidManifest.xml to listen to broadcasts if a new app is installed. Like this:

<receiver android:name=".PackageReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

This class then gets called when a new package is installed:

public class PackageReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // handle install event here
  }
}
Maurene answered 7/7, 2011 at 12:0 Comment(2)
OH yeah that's what we ended up doing back then, thanks, this is the correct solutionEzzo
Thanks uwe. That helped me very much. Is there a way from which we can pass "Intend Extras"(additional info) to the "PackageReceiver" class. In my app I am triggering the installation process. I need to know which app got installed.Hale

© 2022 - 2024 — McMap. All rights reserved.