Android Question Converting StartServiceExact to StartReceiverAt

Azhar

Active Member
Licensed User
Longtime User
Hi,

I have an alarm/event type of app that successfully fires the service exactly on time every time but it is using the StartServiceExactAt with all the relevent user permissions and manifest entry etc. I read that we should now be using receiver modules instead to perform this task. I have converted the service into a receiver module and call it with StartReceiverAt method but it doesn't trigger on-time if at all.
I suspect that the permission is different for services and receivers??

In this code below, what permission do I ask the user for if using receivers not services?

Service Alarm converted to Receiver:
Sub configureNextService(notifyFor As String, inTime As Double)
    Dim eventAlarmTime As Long
    eventAlarmTime = getTimeInTicks(inTime)
    
    Wait For (GetScheduleExactAlarmPermission) Complete (HasPermission As Boolean)
    If HasPermission Then
        Log("Permission Granted for Alarm")
'        StartServiceAtExact(alarmCallService, eventAlarmTime,True)
        StartReceiverAt(alarmCallReceiver, eventAlarmTime, True)
        StartReceiver(widgetReceiverModule)
        Log("Set Alarm Receiver for " & notifyFor & " at " & inTime & " as ticks..." & eventAlarmTime)
        comCode.writeToLog("Starter> set next alarm receiver for " & inTime & " on " & comCode.makeTimeDateStamp)
        gblStrNextDongTime = NumberFormat2(inTime,2,2,2,False)
    Else
        Log("no SCHEDULE_EXACT_ALARM permission")
    End If

Actual Permission Request (for Service Alarm):
Private Sub GetScheduleExactAlarmPermission As ResumableSub
    Dim p As Phone
    If p.SdkVersion >= 31 Then
        Dim am As JavaObject = GetAlarmManager
        If am.RunMethod("canScheduleExactAlarms", Null).As(Boolean) = False Then
            Dim in As Intent
            in.Initialize("android.settings.[B]REQUEST_SCHEDULE_EXACT_ALARM[/B]", "package:" & Application.PackageName)
            StartActivityForResult(in)
            Wait For ion_Event (MethodName As String, Args() As Object)
            Return -1 = Args(0)
        End If
    End If
    Return True
End Sub

This line in particular: in.Initialize("android.settings.REQUEST_SCHEDULE_EXACT_ALARM", "package:" & Application.PackageName)

Kind regards,

Azhar
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Top