Android Question Background Task goes to sleep

Angel Maza

Member
Licensed User
Longtime User
I have been using for a while one app (SENDER) that connects to a Bluetooth device and sends data to a second app (RECEIVER) using Broadcast receiver/intents. The SENDER app runs in the background and the RECEIVER app interacts with the User in the foreground. All worked fine until Android 8.0 came around. Now my SENDER app goes to sleep after some time and the Broadcast Receiver task stops. As soon as I bring the SENDER app the Background Task resumes and the RECEIVER app gets the data again.

How can I stop the SENDER app being put to sleep by the Android OS in an effort to save power?

Attached is a screen shot that shows stopping of the "backgroundservice" at 09:25:02.435 and the regular (red) onReceive() log entries to stop.

Here is my b4a broadcast task as BackgroundService:
B4X:
'--------------------------------------------------------------------------------------------------
' Background Services
'    they will execute every Starter.broadcastFrequency milli-seconds
'    they will be terminated when Starter.runBackgroundTasks is set the False
'    they are started by settin Starter.runBackgroundTasks to True and calling StartService(BackgroundService)
'
' Developed by Volker Petersen ([email protected])
' July 2018
' -------------------------------------------------------------------------------------------------
' https://www.b4x.com/android/forum/threads/send-data-to-app-by-intent.43570/
'
#Region  Service Attributes
    #StartAtBoot: False   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public API As Intent
    'Public intentObject(1) As String Array
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    ' Broadcast Intents will transmit the data in 'sensorData' to other Apps
    
    ' using a Timer is causing an App shutdown. Not yet fully investigated
    'Public broadcastTimer As Timer
    'broadcastTimer.Initialize("broadcastNow", Starter.broadcastFrequency)
    'broadcastTimer.Enabled = True
    Do While(Starter.runBackgroundTasks)
        If (Starter.bleConnected ) Then
            Send_Broadcast_Intents
        End If
        Sleep(Starter.broadcastFrequency)
    Loop
    'broadcastTimer.Enabled = False
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub


Sub broadcastNow_Click
    If (Starter.bleConnected ) Then
        Send_Broadcast_Intents
    End If
End Sub

Sub Send_Broadcast_Intents
    Dim i As Int
    Dim str As String
    
    API.Initialize("com.calypso.api.ACTION_DATA_AVAILABLE", "")
    For i=0 To Starter.dataFieldsAPI.Size-1
        str = Round2( Starter.sensorDataProcessed.Get(Starter.dataFieldsAPI.Get(i)), 3)
        API.PutExtra(Starter.dataFieldsAPI.Get(i), str )
    Next
    Starter.phoneManager.SendBroadcastIntent(API)

    Starter.api_ctr = Starter.api_ctr + 1
    'Log("BackgroundServices->Send_Broadcast_Intents: " & Starter.api_ctr & ".) send:" & Starter.dataFieldsAPI.Size&" fields")

End Sub

Any suggestions of what I can do to stop the Android OS from putting my backgroundservice to sleep?

Thanks

error_log.PNG
 
Top