Android Question Re-Starting app after updating

walterf25

Expert
Licensed User
Longtime User
Hi guys, how can I re-start the app automatically once the app is updated, when it finishes updating the app shuts down, so how can i re-launch the app automatically,

I've tried running another service named RestartActivity and check for the StartingIntent, but it doesn't seem to work for me.

I've added this code to the manifest file:

B4X:
AddReceiverText(restartactivity, <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" />
</intent-filter>)

I can see the service starting but only after I re-install the app through my usb cable, this is the logs i see

** Service (restartactivity) Create **
** Service (restartactivity) Start **
intent: (Intent) Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:ifims.observer flg=0x4000010 cmp=ifims.observer/.restartactivity$restartactivity_BR bqHint=4 (has extras) }
starting intent: package:ifims.observer
extras: Bundle[{android.intent.extra.UID=10214, android.intent.extra.REPLACING=true, android.intent.extra.user_handle=0}]
restarting main Activity...
Activity (main) Pause, UserClosed = false **

But i would like the app to be restarted right away after updating the app.

Below is the code for the RestartActivity service

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("intent: " & StartingIntent)
    Log("starting intent: " & StartingIntent.GetData)
    Log("extras: " & StartingIntent.ExtrasToString)
    If StartingIntent.Action = "android.intent.action.PACKAGE_REPLACED" And IsPaused(Main) Then
        Log("restarting main Activity...")
            MyAppReload
    Else
        StartServiceAt("", DateTime.Now + 5 * 1000, True)
    End If
End Sub

Sub Service_Destroy

End Sub

Sub MyAppReload
  If IsPaused(Main) Then
      Log("restarting Main Activity...")
    StartActivity(Main)
    StopService("")
  End If
End Sub

Any advise on this?

Regards,
Walter
 

DonManfred

Expert
Licensed User
Longtime User
When you update your own app:
Android will kill your running instance and all Services and all shedules services (as far as i know)
The app is replaced then. After Android has installed the app - you can imaginge - your app does not run anymore and even can not check the starting intent because android does not restart your app.

I can imagine it could work using a 2nd app. A watchdog for your app. This app uses a servide and a broadcast receiver which listens to the package replaced broadcast.
After the watchdog receives the broadcast watchdog start your app using an Intent.

PD: Creating a new thread for this question is most probably the correct way. I agree with @ronell ;-)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
When you update your own app:
Android will kill your running instance and all Services and all shedules services (as far as i know)
The app is replaced then. After Android has installed the app - you can imaginge - your app does not run anymore and even can not check the starting intent because android does not restart your app.

I can imagine it could work using a 2nd app. A watchdog for your app. This app uses a servide and a broadcast receiver which listens to the package replaced broadcast.
After the watchdog receives the broadcast watchdog start your app using an Intent.

I Just saw a thread where Erel suggests the broadcast receiver in the manifest, which is the same code i just posted above, the problem here is that the service should get started after the app is updated, in my case i don't see it happening.

PD: Creating a new thread for this question is most probably the correct way. I agree with @ronell ;-)
This is a new Thread ;)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Try it with this intent filter:
B4X:
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
Hi Erel, I tried as you suggested, but it doesn't seem to work, if I leave the intent filter without the "MY" word, i can see the service start but only after i re-install the app.
Any thoughts?

Walter
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
To start the service (app) after it is reinstalled?
The wanted result is to start a service (app) after the service (app) updates itself using downloading a new APK and use an intent (i think) to install the new version using
B4X:
Public Sub InstallApk
  Log("---- AppUpdating.InstallApk")
    'intent to install
  Dim i As Intent
  i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirDefaultExternal, "tmp.apk"))
  i.SetType("application/vnd.android.package-archive")
  StartActivity(i)
  sStatusCode = OK_INSTALL
    If sVerbose Then Log(TAB & "user asked to install newer apk")
    Finito
End Sub
After the app is installed by the systems packageinstaller the apps services no longer works as the app must be started manually once.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Not sure that I understand. Isn't it the desired behavior? To start the service (app) after it is reinstalled?
Hi Erel, sorry for the confusion.

So what i need to do but starting to realize that it may not be possible is the following.

I check if there is an update from a server, if there is an update, the new app version is downloaded, once the download is finished, the downloaded new version is installed, the problem is that after the new version is installed, the app closes and as @DonManfred mentioned, it seems that all processes are killed.

what i have noticed is that if i start a different app with different package name etc.. I can see the starting intent being "PACKAGE_REPLACED", in this case the app that was updated gets relaunched, but what i would really like to do, is restart the app after being updated from within the same app, even after it has been closed.

Hope this makes sense Erel, made myself dizzy trying to explain.

Regards,
Walter
 
Upvote 0
Top