Android Question ServiceStartAt - possible solution for TargetSDKVersion 19 and above

Discussion in 'Android Questions' started by wes58, May 20, 2015.

  1. wes58

    wes58 Active Member Licensed 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?
     
  2. wes58

    wes58 Active Member Licensed User

    And here is a sample code that can be used to start the service
    Code:
    '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.
     
    inakigarm and DonManfred like this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    It will be added in the next version.
     
    lemonisdead and DonManfred like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice