Android Question StartActivity from Service

Scantech

Well-Known Member
Licensed User
At boot startup, my service need to call Main if enabled by user. I saw hint with StartActivity stating "you should usually not call StartActivity from Service". How else am i going to start up Main? Is StartActivity the best method?
 

DonManfred

Expert
Licensed User
Call a sub in your activity with CallSubDelayed. It will start your activity and call the sub.

Note that you should NOT do that right directly. at boot. Maybe you need to wait a few seconds.
 

Erel

Administrator
Staff member
Licensed User
Note that starting from Android 10 it is no longer possible to start activities while the app is in the background (foreground service will not help here).
 

OliverA

Expert
Licensed User
Note that starting from Android 10 it is no longer possible to start activities while the app is in the background (foreground service will not help here).
One (of many) exception to this restriction mentioned in the Android docs (here: https://developer.android.com/guide/components/activities/background-starts) is:
The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.
And (not unusual) @Erel has already supplied code for how to do this here: https://www.b4x.com/android/forum/threads/draw-on-top-of-other-apps-permission.90513/
This solutions has been tested and confirmed to be working by @Jmu5667

Note: For completeness: This particular exception has another exception:
 

Jmu5667

Well-Known Member
Licensed User
This solutions has been tested and confirmed to be working by @Jmu5667
Yes indeed, you need to add this to the manifest file.

B4X:
AddPermission(android.permission.SYSTEM_ALERT_WINDOW)
And this

B4X:
Sub Activity_Resume
 
   Dim p As Phone
 
   If p.SdkVersion > 28 Then
       Dim c As cls_request_drawover_persmission
       c.Initialize
       Wait For (c.GetPermission) Complete (Success As Boolean)
   End If

end sub
cls_request_drawover_permission @OliverA

B4X:
Sub Class_Globals

   Private ion As Object

End Sub

Public Sub Initialize

End Sub

Public Sub GetPermission As ResumableSub

   Dim jo As JavaObject
   jo.InitializeStatic("android.provider.Settings")
   Dim ctxt As JavaObject
   ctxt.InitializeContext
 
   ' // we already have it
   If jo.RunMethod("canDrawOverlays", Array(ctxt)) = True Then
       Return True
   End If
 
   ' // request it
   Dim i As Intent
   i.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION", "package:" & Application.PackageName)
   StartActivityForResult(i)
   Wait For ion_Event (MethodName As String, Args() As Object)
   Return jo.RunMethod("canDrawOverlays", Array(ctxt))

End Sub

Private Sub StartActivityForResult(i As Intent)
 
   Dim jo As JavaObject = GetBA
   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
 
End Sub

Private Sub GetBA As Object
 
   Dim jo As JavaObject = Me
   Return jo.RunMethod("getBA", Null)
 
End Sub
Obviously credit to @Erel for providing the necessary code etc.

Regards

John
 
Last edited:

Erel

Administrator
Staff member
Licensed User
There is no such permission: AddPermission(android.permission.SYSTEM_OVERLAY_WINDOW)
 
Top