Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android programmatically update apk and see the result of the installation

I'm writing an app updater for my app. After I make sure I have my apk on the device, this is what I do from within the app I'm trying to update:

Intent promptInstall = new Intent(Intent.ACTION_VIEW);
File f = new File(apkLocation);    
promptInstall.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
_context.startActivity(promptInstall);

This launches my installer which displays the app permissions and I am able to click "Install". But from here the app simply closes, I get no message whatsoever (I would've expected the dialog telling me the install was successful giving me the option to press "Close" or "Open"). It just goes to the main screen of the device without further notice.

On a side note, the app is indeed updated when I manually open it back. How can I make the installer go all the way as expected? Is there anything to set on the intent?

While writing this, I'm wondering if the reason this happens is that the current app is simply overwritten on the device thus closing it and by extent not getting the result of the intent because it's source was killed?

like image 427
Florin Bombeanu Avatar asked May 15 '13 15:05

Florin Bombeanu


People also ask

How do I notify people of an Android app update?

You do not need to do anything specific for this. Since you mentioned that you are using Google Play, the update notification is taken care of by Google Play. You just need to update the APK with a higher versionCode and Google Play should do the rest.

How do I know if APK is installed?

If you want to locate the APK files in your Android phones, you can find the APK for user-installed apps under /data/app/directory while the preinstalled ones are located in /system/app folder and you can access them by using ES File Explorer.


2 Answers

All you can do is register a receiver with the intent filters like android.intent.action.PACKAGE_INSTALL or android.intent.action.PACKAGE_REPLACED from which you can restart your application back again.

<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <data android:scheme="package"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_CHANGED"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED"/>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>
</application>

And

public class BootService extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
        Intent serviceIntent = new Intent();
        serviceIntent.setClass(context,Controller.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(serviceIntent);
    } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
        Intent serviceIntent = new Intent();
        serviceIntent.setClass(context, Controller.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(serviceIntent);
    }
  }
}
like image 107
Bishwash Avatar answered Oct 23 '22 04:10

Bishwash


To successfully update you need to launch intent with URI indicating to your update app as new task.

 final Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK));
 "application/vnd.android.package-archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

My post below:

Android application update issue

like image 33
dawid gdanski Avatar answered Oct 23 '22 04:10

dawid gdanski