Android Code Snippet Start receiver at exact time

The standard StartReceiverAt is inexact, in order to allow the OS to make optimizations.

Worth reading:

Exact scheduling:
1. AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
2. Private ion As Object - class global

Private Sub Button1_Click
    Wait For (GetScheduleExactAlarmPermission) Complete (HasPermission As Boolean)
    If HasPermission Then
        StartServiceAtExact(ScheduledReceiver, DateTime.Now + 10 * DateTime.TicksPerSecond, True) 'yes it is StartServiceAtExact and we pass a receiver
        Log("no permission")
    End If
End Sub

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.REQUEST_SCHEDULE_EXACT_ALARM", "package:" & Application.PackageName)
            Wait For ion_Event (MethodName As String, Args() As Object)
            Return -1 = Args(0)
        End If
    End If
    Return True
End Sub

Private Sub GetAlarmManager As JavaObject
    Dim ctxt As JavaObject
    Return ctxt.RunMethod("getSystemService", Array("alarm"))
End Sub

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

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub


    15.1 KB · Views: 100


Well-Known Member
Licensed User
Longtime User
How can I use with this?: