Android Question StartActivity from Service

Scantech

Well-Known Member
Licensed User
Longtime 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?
 

OliverA

Expert
Licensed User
Longtime 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:
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime 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:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Am I correct in thinking that if I set my app's TargetSDK to 22, then I can freely start an activity from a service and won't need to use this code?

NOTE: The app will NOT be in the play store.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Am I correct in thinking that if I set my app's TargetSDK to 22, then I can freely start an activity from a service and won't need to use this code?

NOTE: The app will NOT be in the play store.
I don't really know. You can try it. Personally, I would adhere to the rules and move to the latest SDK, implement the permissions. Is this for a customer or personal use ?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
This is for an app running on FireOS, which is not a full-fledge android version because it doesn't offer a UI setting to enable or deny draw-over-other-apps permission on a per-app basis.

So, I need to keep the target SDK below 23.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
This is for an app running on FireOS, which is not a full-fledge android version because it doesn't offer the user a UI setting to enable or deny draw-over-other-apps permission on a per-app basis.

So, I need to keep the target SDK below 23.
sure, just try it.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…