Android Code Snippet Receive package update

Code to start the app automatically if it was updated

In the Manifest add:
B4X:
' ---. ---. ---. ---. ---. ---. ---. ---. ---.
' Start service if app was updated
' ---. ---. ---. ---. ---. ---. ---. ---. ---.
' Erel --> https://www.b4x.com/android/forum/threads/re-starting-app-after-updating.79407/#post-502898
' androiddev --> https://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED

' androiddev --> https://developer.android.com/about/versions/oreo/background.html
AddReceiverText(svcappverisnew, <intent-filter>
                                    <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                                </intent-filter>)
AddPermission("android.permission.RECEIVE_BOOT_COMPLETED") ' so --> https://stackoverflow.com/questions/49766775/how-to-run-a-service-in-background-after-updating-the-app
AddPermission("android.permission.BIND_JOB_SERVICE") ' so --> https://stackoverflow.com/questions/46532190/android-8-0-broadcastreceiver-not-receiving-after-a-reboot
' ---. ---. ---. ---. ---. ---. ---. ---. ---.

Add service "svcappverisnew" to the project:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
End Sub
Sub Service_Create
End Sub

' Inspired by walterf25 --> https://www.b4x.com/android/forum/threads/re-starting-app-after-updating.79407/
Sub Service_Start (StartingIntent As Intent)
    Log("#-Service_Start")
    Log("#-  intent: " & StartingIntent)
    Log("#-  starting intent: " & StartingIntent.GetData)
    Log("#-  StartingIntent.Action: " & StartingIntent.Action)
    Log("#-  extras: " & StartingIntent.ExtrasToString)
    If StartingIntent.Action = "android.intent.action.MY_PACKAGE_REPLACED" And IsPaused(Main) Then
        Log("#-    x18, MY_PACKAGE_REPLACED!")
       
        '        Dim n As Notification
        '        billing.CreateNotificationChannel(n.IMPORTANCE_HIGH)
        '        n.Initialize2(n.IMPORTANCE_HIGH)
        '        n.Icon = "icon"
        '        n.SetInfo("testtitle 'new content available'", "testbody", Main)
        '        n.Notify(1)
       
        'MyAppReload
    Else
        StartServiceAt("", DateTime.Now + 5 * 1000, True)
    End If
End Sub

Sub Service_Destroy
End Sub
'
'Sub MyAppReload
'    Log("#-MyAppReload")
'  If IsPaused(Main) Then
'      Log("#-  x40, restarting Main Activity...")
'    StartActivity(Main)
'    StopService("")
'  End If
'End Sub


B4X:
04-12 15:04:16.397: I/B4A(16924): ~i:** Receiver (svcappverisnew) OnReceive **
04-12 15:04:16.544: I/B4A(16924): ~i:*** Service (svcappverisnew) Create ***
04-12 15:04:16.544: I/B4A(16924): ~i:** Service (svcappverisnew) Start **
04-12 15:04:16.549: I/B4A(16924): #-Service_Start
04-12 15:04:16.549: I/B4A(16924): #-  intent: (Intent) Intent { act=android.intent.action.MY_PACKAGE_REPLACED flg=0x4000010 pkg=com.yyyy.zzz cmp=com.yyyy.zzz/.svcappverisnew$svcappverisnew_BR (has extras) }
04-12 15:04:16.549: I/B4A(16924): #-  starting intent: null
04-12 15:04:16.549: I/B4A(16924): #-  StartingIntent.Action: android.intent.action.MY_PACKAGE_REPLACED
04-12 15:04:16.549: I/B4A(16924): #-  extras: Bundle[{android.intent.extra.user_handle=0}]
04-12 15:04:16.549: I/B4A(16924): #-    restarting main Activity...
04-12 15:04:16.549: I/B4A(16924): #-MyAppReload
04-12 15:04:16.550: I/B4A(16924): #-  x40, restarting Main Activity...
04-12 15:04:16.559: I/B4A(16924): ~i:*** Service (starter) Create ***
04-12 15:04:16.608: I/B4A(16924): Device locale: de
04-12 15:04:16.637: I/B4A(16924): ~i:** Service (starter) Start **
04-12 15:04:16.668: I/B4A(16924): ~i:** Service (svcappverisnew) Destroy **
04-12 15:04:16.692: I/B4A(16924): ~i:** Activity (main) Create, isFirst = true **
04-12 15:04:16.722: I/B4A(16924): ~i:** Activity (main) Resume **
04-12 15:04:17.933: I/B4A(16924): #-  x28, --------- --------- --------- --------- --------- --------- --------- --------- ---------
04-12 15:04:17.933: I/B4A(16924): #-  x28, Main.Activity_Resume, Intent handling iii iii iii iii iii iii iii iii iii iii iii
04-12 15:04:17.933: I/B4A(16924): #-    x29, intActIntent.Action=
04-12 15:04:17.933: I/B4A(16924): #-    x30, intActIntent.ExtrasToString=no extras
04-12 15:04:17.933: I/B4A(16924): #-    x31, intActIntent.GetData=null
 
Top