Android Question Use of SetExactAndAllowWhileIdle


Licensed User
Longtime User
My application that worked well with android 5 no longer works with android 7.

The StartServiceAt function no longer works after about one hour of use: sleep mode.

I want to use SetExactAndAllowWhileIdle instead of StartServiceAt but it seems that I missed something because the program starts but does not restart Service_start after the requested 10 minutes.

Can you help me.
Thank you

Sub Service_Create                                                        ' Début ou retour du prog
    Tdf.Initialize                                                        ' Init de la notification
    Tdf.Icon = "iconb"                                                    ' Définition de l'icone
    Tdf.Sound = False                                                    ' Pas de son notification
    Tdf.OnGoingEvent = True                                                ' Signalement de la notification
    Tdf.SetInfo ("Tdf","Tâche de fond Test","")                            ' Nom de la tâche
    DateDebut = DateTime.Now
    Service.StartForeground(2,Tdf)                                        ' Service en tâche de fond
    Delay = 600000                                                        ' Essai à 10mn
End Sub

Sub Service_Start (StartingIntent As Intent)                            ' Reveil du service StartServiceAt
    NbMesure = NbMesure + 1                                                ' Incrémenter le nombre de mesures
    DateNow = DateTime.Now                                                ' Date actuelle
    Reveil = DateDebut + (Delay * (NbMesure))
    Log("Start: Index= "&NbMesure&" Actuel: "&DateTime.time(DateNow)&" Date début: "&DateTime.time(DateDebut)&" Réveil à: "&DateTime.Time(Reveil))    ' Imprimer
End Sub

Sub Service_Destroy                                                        ' Sortir de la tâche
    'Service.StopForeground(Me)                                            ' Arrêt tâche de fond
End Sub

Sub SetExactAndAllowWhileIdle (Time As Long, ServiceName As String)
    Dim p As Phone
    If p.SdkVersion < 23 Then
        StartServiceAtExact(ServiceName, Time, True)
        Dim in As Intent
        in.Initialize("", "")
        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
        Dim ctxt As JavaObject
        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
        Dim pi As JavaObject
        pi = pi.InitializeStatic("").RunMethod("getService", _
       Array(ctxt, 1, in, 134217728))
        am.RunMethod("setExactAndAllowWhileIdle", Array(0, Time, pi))
    End If
End Sub


Licensed User
Longtime User
Thanks Erel,

Has worked all night without a problem.
I thought we could write "" as for StartServiceAt.

Thank you again for your help
Upvote 0