0

I am new to Android and I have an application which consists in a service and on some point the service needs to install a new .apk (basically an auto-update), currently the installation is done like in the code bellow which does not allow to know when it finishes or get a result out of it and I need to know that in order to perform other actions driven on that result.

File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
        .setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);

I would like to know if there is a way of doing that? Thank you in advance.

Boscaiolo
  • 3
  • 1
  • 4
  • Check out this: http://stackoverflow.com/questions/4604239/install-application-programmatically-on-android Does it help? – Jake Chasan Jun 26 '14 at 14:03
  • I did see this already, they are doing it in the same way with FLAG_ACTIVITY_NEW_TASK set on the intent which by itself does not allow to have a result back and returns -1 immediately. – Boscaiolo Jun 26 '14 at 14:11

1 Answers1

0

The Logic Could Be:

  1. Install Script
  2. Wait 5 seconds
  3. Run the appInstalledOrNot function
  4. If not installed, then try again in 1 second

Here could be the code:

public void installApp(){
    File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
    Intent promptInstall = new Intent(Intent.ACTION_VIEW)
            .setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    appContext.startActivity(promptInstall);

    Thread t = new Thread(){
        public void run(){

            boolean installed = checkInstalled("vnd.android.package-archive");
            while(!installed)
            {
                System.out.println("Not Installed Yet..Waiting 1 Second");
                t.sleep(1000);
            }

            //Now it has been installed
            if(installed) {
                //This intent will help you to launch if the package is already installed
                Intent LaunchIntent = getPackageManager()
                    .getLaunchIntentForPackage("com.Ch.Example.pack");
                startActivity(LaunchIntent);

                System.out.println("App already installed on your phone");        
            }
            else {
                System.out.println("App is not installed on your phone");
            }
            }
    t.start();
}

private boolean checkInstalled(String uri) {
    PackageManager pm = getPackageManager();
    boolean app_installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    }
    catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed ;
}

Code based on: How to check programmatically if an application is installed or not in Android?

Community
  • 1
  • 1
Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
  • Yeah I am trying it out, Thank you for your answer. I am just wondering how to make it work because all of my stuff already happens in a new thread run() and I am trying to combine the two and also I would like to avoid using thread.sleep(). – Boscaiolo Jun 26 '14 at 15:54
  • The only thing that is running on the thread is the while loop, so shouldn't the rest of your application remain unaffected? – Jake Chasan Jun 26 '14 at 16:00
  • Yeah, this is a fair point, I will do some experiments and tests and see what happens. – Boscaiolo Jun 26 '14 at 16:12