Android Question CancelScheduledService - Does not seem to canel ?

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi


The following code is for scheduling a service:

B4X:
Sub schedulecallback(timeToAlarm As Long)
   
   Dim p As Phone

   ' // zero check
   If timeToAlarm = 0 Then
       Return
   End If

   
   If p.SdkVersion >= 23 Then
       Dim srvAt As JavaObject
       srvAt.InitializeContext
       srvAt.RunMethod("StartServiceAtExactWhileIdle", Array(timeToAlarm, True))
   Else
       'mod_functions.writelog("svc_watchdog(), call back using StartServiceAtExact()")
       StartServiceAtExact(Me,timeToAlarm,True)
   End If
   
End Sub


#If JAVA
   //import android.app.Service;
   import android.app.AlarmManager;
   import android.app.PendingIntent;
   import android.content.Context;
   import android.content.Intent;
   import android.os.PowerManager;

   public void StartServiceAtExactWhileIdle(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.setExactAndAllowWhileIdle(wakeUp?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, time, penInt);
   }
#End If

When the app is exiting, the CancelScheduledService does not seem to do the job, as the service start again.

Any idea's ?

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Why are you using inline Java for this? You should use the built-in StartServiceAtExact.

Was there not an issue with this before where the service's did not start at the correct time and this code was published to overcome it, I remember some developers had problems with Alarms not being triggered ?

I will do as you suggest, but is there an issue with the java code I am using and cancelScheduledService ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but is there an issue with the java code I am using and cancelScheduledService ?
Yes. It is incorrect.

Was there not an issue with this before where the service's did not start at the correct time and this code was published to overcome it
This was fixed several versions ago.
 
Upvote 0
Top