android programmatically update apk and see the result of the installation

前端 未结 3 2005
耶瑟儿~
耶瑟儿~ 2020-12-13 20:23

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 pr         


        
相关标签:
3条回答
  • 2020-12-13 21:10

    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

    0 讨论(0)
  • 2020-12-13 21:18

    First off, you can't install without prompt, unless you are rooted or have system privileges. I don't think you were asking that, but one of your paragraphs isn't clear.

    Secondly, if installing an update version of a running app, the behavior you're seeing is expected: The app is force-closed and updated. You can't update in-place. You can detect when the installation was aborted, because the activity invoking the installer will be resumed.

    In order to update a running app AND keep it running, you'll need a separate process (app) to monitor the installation and restart your app.

    0 讨论(0)
  • 2020-12-13 21:19

    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);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题