14

I have tried many ways given in StackOverFlow and other website but it doesn't really work.

My issue is that I have this application that I need to update and after updating, it should automatically turn the same application (updated) back on.

This is my code:

private synchronized void runRootUpdate() {    
    // Install Updated APK
    String command = "pm install -r " + downloadPath + apkFile;
    Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
    int test = proc.waitFor(); // Error is here.

    if (proc.exitValue() == 0) {
        // Successfully installed updated app
        doRestart()
    } else {
        // Fail
        throw new Exception("Root installation failed");
    }
}

private void doRestart() {
    Intent mStartActivity = new Intent(context, MainActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

Take a look at my "Error is here." My old app will installed a new updated app and kill itself, thus there is no proc.waitFor() and end up back in the home screen BUT the new updated app is being installed. However, I need it to turn it back on itself. Is there any way I could do that?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Boon Jun Tan
  • 141
  • 1
  • 1
  • 4

5 Answers5

7

Rather than setting an alarm, have a Manifest registered BroadcastReceiver specifically for this purpose. It can either listen to android.intent.action.PACKAGE_REPLACED (note: the word action is missing from this constant) or to android.intent.action.MY_PACKAGE_REPLACED

<receiver android:name="...">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
        <data android:scheme="package" android:path="com.your.package" /> 
    </intent-filter>
</receiver>

After reinstall your receiver will get this message and you'll be able to start an Activity

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

Updating an application involve killing all its processes. You should set the alarm before launching the pm command (with more than a 100 ms delay)

bwt
  • 17,292
  • 1
  • 42
  • 60
0

First of all you need to know where to store your .apk file because that is important while upgrading the application.

To upgrade and launch application silently you need to follow following steps.

Ensure that you have latest .apk file in your sdcard while upgrading your application.If it is having then launch it via intent like below for upgrade

File file = new File(Environment.getExternalStorageDirectory(), "app-debug.apk"); // mention apk file path here
                if (file.exists()) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    startActivity(intent);
                } else {
                    Toast.makeText(UpdateActivity.this, "file not exist", Toast.LENGTH_SHORT).show();
                }

Above code will upgrade your application with default behavior of android.

Create a broadcast receiver which will know when upgrade is performed

    <receiver android:name=".receiver.OnUpgradeReceiver">
                <intent-filter>
// Note: This intent can is available starting from API 12
                    <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                </intent-filter>
            </receiver>

if you are using api > 12 use

<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                    </intent-filter>

if you are having api <= 12 use

  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" android:path="your.app.package" />

Create a broadcast receiver for getting broadcast after update and launch activity which you want

Example :

public class OnUpgradeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
       // launch your fav activity from here.
    }
}

Above broadcast receiver helps you to launch application silently.

Hope these help you.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

Hmm... Why to use this hard metod, when you are using root installation? Why not to try creating simple shell script to update app? Then you don´t must set alarm and doing other things:

Script (How to pass arguments, How to start app from terminal):

am kill com.myapp.package
pm install -r $1
am start -n packageName/pkgName.ActivityName

App code:

String command = "sh script.sh " + downloadPath + apkFile;
Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
Community
  • 1
  • 1
Samuel Tulach
  • 1,319
  • 13
  • 38
0

use this Play Core Library In-app updates to achieve this

dependencies {
implementation 'com.google.android.play:core:1.5.0'
...}
Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53