Canceling alarm manager when service has finished - android

I want to cancel my alarm-manager, which starts a service, and which I created using following code:
Intent intent = new Intent(getActivity(), CheckStatusService.class);
pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
cal.add (Calendar.MINUTE,1);
alarm = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
I am doing
alarm.cancel(pintent)
and it is not throwing any errors, but the service I started using this Alarmmanager still continues to work.
Once the service is finished, it sends out a broadcast to one of my activities. There it is definitively received, even alarm.cancel() is called, but the service still continues to run.
What am I doing wrong?

I solved my issue by cancelling the alarm within the service by using the following code:
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), CheckStatusService.class);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pintent);

Related

Start of activity through AlarmManager PendingIntent after clearing app process

My app will allow user to set desired time on which activity will trigger. I used AlarmManager to open activity. Below is the code for same.`
/* Intent intent = new Intent(this, AlarmNotificationActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getBaseContext(), RQS_1, intent,
0);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),interaval, pendingIntent);*/
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent,
0);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),interaval, pendingIntent);
If I use the PendingIntent with getActivity even the app killed AlarmNotification is came up.
If I use the PendingIntent with getBroadcast if the app killed AlarmNotification is not coming up.
Question is After clearing the app process from task list
Is it possible to trigger Notification Activity of my app through broadcast.
If Yes - Please share any link or sample code (TIA)
If No - How Clock App in Mobile triggers Alarm on specific time. Do I
need to define service to achieve this.

AlarmManager cancel() does not stop my Alarm

I'm creating the alarm with this code when my widget is added to screen:
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WidgetAlarmReceiver.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 60000, 60000, alarmIntent);
And I'm deleting the alarm using this code when no more widgets are present on the screen. I'm calling this code on the onReceive method of the AlarmReceiver class. Maybe that is the problem?
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent mIntent = new Intent(context, WidgetAlarmReceiver.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
alarmMgr.cancel(alarmIntent);
The problem is that the alarm is still getting called each minute even doing the cancel call.
What is wrong? The startAlarm code is not getting called anymore so I don't understand why the cancel doesn't works and the alarm gets called again and again.
Finally I solved it, by canceling the alarm from outside the alarm onReceive method itself. Maybe an alarm cannot cancel itself or something.

Alarm preventing activity from finish

I am using Alarm service in an Android application and here is the code snippet for same
Creation of Pending Intent
Intent intent = new Intent(context, UninstallService.class);
intent.putExtra("APPLICATION_PREFERENCE", applicationPreference);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(context,
intentRequestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Scheduling of an Alarm
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
After scheduling the alarm, I invoke finish on Activity, but the activity doesn't finish. It stays open and there is no clue what is happening. If I comment the call to set method on AlarmManager and invoke finish on Activity, the activity is closed.
This is bit weird and I am not sure, what I am missing. Please help.
Android OS : Lollipop (5.0.2 - API 21)
Try this code.
Intent intent = new Intent(context, UninstallService.class);
intent.setAction(intentAction);
intent.putExtra("APPLICATION_PREFERENCE", applicationPreference);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr .setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent );

Run Broadcastreceiver for specified time

I am new to android, I want to run a task only for a specific duration, how can I do this?
I tried using broadcast receiver and AlarmManager but the broadcastreceiver continuously runs even after specified time.
long timetorun= calendarcurrent.getTimeInMillis() - calendar.getTimeInMillis();
Intent c = new Intent(getApplicationContext(),
Phonecallreceiver.class);
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, timetorun, pintent);
You can stop it by canceling the alarm, using the same PendingIntent like the below:
Intent c = new Intent(getApplicationContext(), Phonecallreceiver.class);
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm .cancel(pintent);
Do not forget to make sure that the PendingIntent is not NULL.
Hope that I helped you.

How to stop Service using Alarm Manager at a particular time in Android?

In my app i want to stop the service in a particular time using Alarm Manager. But Alarm Manager cancel() method is not taking any time. So how can i stop it?
I wrote the code like below:
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(DashboardScreen.this, req, intent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),30*1000, pi);
Log.d("Testing", "Intent created");
Intent intent1 = new Intent(this, ServiceClass.class);
PendingIntent sender = PendingIntent.getService(this, req, intent1, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
In my opinion, you cannot stop a service using alarmmanager in a direct way. cancel() function in alarmmanager is just to remove the scheduled repeating alarm.
I suggest you to call an activity which stops your service using another alarm manager when needed:
alarmmanager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+TIME_PERIOD,pendingIntent);
If you want to stop your service after 600 seconds, you can set TIME_PERIOD to 600*1000.
pendingIntent is for activity that stops your service.

Categories

Resources