I want to use the Alarm Manager to perform a recurring task while starting my application, but I have a problem canceling the task.
What is my code:
Services:
public class SyncService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("#####", "It alive!"); return Service.START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } }
Application:
public class TerminalApplication extends Application { PendingIntent pintent; AlarmManager alarm; @Override public void onCreate() { super.onCreate(); Intent intent = new Intent(this, SyncService.class); pintent = PendingIntent.getService(this, 0, intent, 0); alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); Log.e("#####", "Starting alarm"); alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), 5 * 1000, pintent); } @Override public void onTerminate() { clear(); super.onTerminate(); } public void clear() { alarm.cancel(pintent); Log.e("#####", "Alarm cancelled"); } }
And this is what I see in the logs:
07-09 14:53:30.399: ERROR/#####(1743): Starting alarm 07-09 14:53:52.690: ERROR/
I use the same intention to cancel the task, even in the same instance, but this does not work. Can you help me?
source share