Android Question ShortService DataSync and Problems with SDK > 34

Magma

Expert
Licensed User
Longtime User
Hi Fam!

Well I thought my problems end when Erel show us the perfect: Service.StopForeground(51042)

but i have some error at Android 15+ devices...

My Service name is "gggService" and i have got permissions after checks from Google for DataSync (Into Service module using some timer_tick to do my API posts)

in manifest i have the:
B4X:
SetServiceAttribute(gggService, android:foregroundServiceType, "dataSync")

And for the Android 15+ using the following as suggested:
B4X:
Private Sub Service_Timeout(Params As Map)

        Service.StopForeground(51042)
        started=0

End Sub


other code into service is that:

B4X:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    Dim Phone As Phone
    aver=Phone.SdkVersion
 
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
 
    If Provider.IsInitialized=False Then Provider.Initialize
 
    xtimer.Initialize("xtimer",20000)
 
 
    If Main.secureit=True Then
        mytimer.Initialize("mytimer",5000)
    End If
 
End Sub

Sub Service_Start (StartingIntent As Intent)
 
    If onlyonetime=0 Then
 
    StartForegroundWithType("...")

    n.Initialize("default", Application.LabelName, "LOW").AutoCancel(True).SmallIcon(LoadBitmap(File.DirAssets,"icon4app.png"))'.LargeIcon(LoadBitmap(File.DirAssets,"icon2.png"))
 
    n.Build("ggg", "Η εφαρμογή εκτελείται!", "tag1",  Main).Notify(1)
    shownot=True

        xtimer.Enabled=True
        If Main.secureit=True Then
            mytimer.Enabled=True
        End If

    End If
 
    onlyonetime=1

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return False
End Sub

Sub delayedstops
 
    If Main.isexited=False Then
    Main.isexited=True
    xtimer.Enabled = False
    mytimer.Enabled = False

    ToastMessageShow("Η εφαρμογή θα κλείσει...",True)

     ' Stop manual foreground mode (and remove the notification)
 
    If shownot=True Then
        started=0
        Service.StopForeground(51042)
        Sleep(500)
        ' Cancel any future scheduled starts
        CancelScheduledService(Me)
        Sleep(500)
    End If
 
    CallSubDelayed2(Main, "ServiceStopped", "gggService")
    End If
 
End Sub


Sub Service_Destroy
    If Main.isexited=False Then
        If shownot=True Then
            started=0
            CallSubDelayed(Me,"delayedstops")
        End If
    End If
End Sub


Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon2"
    notification.SetInfo("ggg", Body,  Main)
    Return notification

End Sub


Sub StartForegroundWithType(notif As Notification)
    Dim oContext As JavaObject
    Dim serviceType As Int = 1
 
    Dim RunningPhone As Phone
 
    oContext.InitializeContext
 
    If RunningPhone.SdkVersion >= 29 And RunningPhone.SdkVersion<35 Then
       
        Try
            oContext.RunMethod("startForeground", Array As Object(1, CreateNotification(notif), serviceType))
            started=1
        Catch
            Log(LastException)
            Service.StartForeground(1, notif)
            started=1
        End Try
    Else
        Service.StartForeground(1,  CreateNotification(notif))
        started=1
    End If
End Sub

Also when exiting my app - from menu I want to stop services if it is possible...
So from b4xpages - calling this:
B4X:
Private Sub exitopt_Click
    'reverse
    Main.inprogress=True
    Main.secureit=False
    Try
    StopService(gggService)
    Catch
        ExitApplication
    End Try
End Sub

and in Main:
B4X:
'to emulate exiting from app - sometimes need to have services running... not stopping the same time..

Sub ServiceStopped(name As String)
        Dim iHome As Intent
        iHome.Initialize("android.intent.action.MAIN", "")
        iHome.AddCategory("android.intent.category.HOME")
        iHome.Flags = 0x10000000   ' FLAG_ACTIVITY_NEW_TASK
        Dim joIntent As JavaObject = iHome
        Dim joContext As JavaObject
        joContext.InitializeContext
        Dim pm As Object = joContext.RunMethod("getPackageManager", Null)
        If joIntent.RunMethod("resolveActivity", Array(pm)) <> Null Then
            StartActivity(iHome)
            Else
            ToastMessageShow("Press Home to exit - Bye bye!",True)
        End If
End Sub


But in Android Vitals, getting many errors in Android 15 about services !!!!!

many of:

Exception android.app.RemoteServiceException$ForegroundServiceDidNotStopInTimeException:
at android.app.ActivityThread.generateForegroundServiceDidNotStopInTimeException (ActivityThread.java:2339)
at android.app.ActivityThread.throwRemoteServiceException (ActivityThread.java:2301)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2616)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loopOnce (Looper.java:232)
at android.os.Looper.loop (Looper.java:317)
at android.app.ActivityThread.main (ActivityThread.java:8849)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:681)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:902)

---------


not so much... (1,2..3):

Exception java.lang.RuntimeException:
at anywheresoftware.b4a.keywords.Common$12.run (Common.java:1268)
at android.os.Handler.handleCallback (Handler.java:959)
at android.os.Handler.dispatchMessage (Handler.java:100)
at android.os.Looper.loopOnce (Looper.java:249)
at android.os.Looper.loop (Looper.java:337)
at android.app.ActivityThread.main (ActivityThread.java:9592)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:593)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
Caused by android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelableInternal (Parcel.java:5089)
at android.os.Parcel.readParcelable (Parcel.java:5071)
at android.os.Parcel.createExceptionOrNull (Parcel.java:3251)
at android.os.Parcel.createException (Parcel.java:3240)
at android.os.Parcel.readException (Parcel.java:3223)
at android.os.Parcel.readException (Parcel.java:3165)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:6780)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1971)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1946)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:848)
at anywheresoftware.b4a.keywords.Common.StartServiceImpl (Common.java:977)
at anywheresoftware.b4a.keywords.Common.StartService (Common.java:964)
at anywheresoftware.b4a.keywords.Common$12.run (Common.java:1236)
 
Top