Android Question ServiceStartAt - possible solution for TargetSDKVersion 19 and above

wes58

Active Member
Licensed User
Longtime User
I have been using ServiceStartAt to start the service and found out that the difference in scheduled and actual start times can be several minutes. See the log below:
After reading about AlarmManager, I have found out that the problem can be with the applications that have TargetSDKVersion 19 or above and use the AlarmManager set function.
The recommended method to use is setExact
I did it using inline Java but the question for Erel is, that maybe he could implement it in the B4A Core.jar for StartServiceAt function?
 

wes58

Active Member
Licensed User
Longtime User
And here is a sample code that can be used to start the service
B4X:
'Declare in Globals    
        Dim srvAtExact As JavaObject

'for example, in your sub where you want to start service 
Sub ServiceSchedule(h as Int, m as Int)
    Dim NextStartTime As Long    'time in ticks for next start

        NextStartTime = DateTime.TimeParse(DateTime.Time(DateTime.Now)) + h * DateTime.TicksPerHour + m * DateTime.TicksPerMinute
        srvAtExact.InitializeContext
        srvAtExact.RunMethod("StartServiceAtExact", Array(NextStartTime, True))
'        srvAtExact.RunMethod("StartServiceAtExact", Array(Me, NextStartTime, True))      
End Sub

#If JAVA
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
    public void StartServiceAtExact(Long time, Boolean wakeUp){
        AlarmManager alMan = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this.getApplicationContext(), this.getClass());
        PendingIntent    penInt = PendingIntent.getService(this.getApplicationContext(), 1, intent, 134217728);
        alMan.setExact(wakeUp?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, time, penInt);
    }
/*
    public void StartServiceAtExact(Class service, Long time, Boolean wakeUp){
        AlarmManager alMan = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this.getApplicationContext(), service);
        PendingIntent    penInt = PendingIntent.getService(this.getApplicationContext(), 1, intent, 134217728);
        alMan.setExact(wakeUp?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, time, penInt);
    }
*/

#End If
Maybe someone will find it useful.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…