Android Question Screen on/off broadcast receiver

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
I am making an app with widget. The data in the widget needs to be updated hourly. As there is an online request necessary I would like to optimize it at far as possible to spare battery and data. So I read about the possibility of a broadcast receiver for detecting screen on off events. I thought of only updating if the screen is turned on and if an hour has passed since last time or something like that.
Is there any info on how to implement this in B4A? What event is that?
I found this on stackoverflow. But how do I transfer it to B4A?

Manifest:
<receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.SCREEN_OFF" />
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>

Update: I tried it with PhoneEvents in a service:

Code:
#Region  Service Attributes
    #StartAtBoot: True
   
#End Region

Sub Process_Globals
    Private pe As PhoneEvents

End Sub

Sub Service_Create
    pe.Initialize("PE")
End Sub

Sub Service_Start (StartingIntent As Intent)
    'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Log("start")
    Log(StartingIntent)
   
End Sub

Sub Service_Destroy
    pe.StopListening
End Sub


Sub PE_ScreenOn(Intent As Intent)
    Log("on")
End Sub

Sub PE_ScreenOff(Intent As Intent)
    Log("off")
End Sub

Added the permissions to the manifest:
AddPermission("android.permission.READ_PHONE_STATE")
AddReceiverText(eventlistener, <intent-filter><action android:name="android.intent.action.SCREEN_OFF"/><action android:name="android.intent.action.SCREEN_ON"/></intent-filter>)

But this did not work.


Thanks
 
Last edited:
Top