Start of activity through AlarmManager PendingIntent after clearing app process - android

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.

Related

How to set an alarm for every 1 minute, for same say only in android?

I made one application with Android native. I'm using AlarmManager for achieving every 1-minute track data.
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),100000,pendingIntent);
But I want that it must stop on specific time like today 12 P.M. or whatever time I want.
How can I achieve it by making another AlarmManager Service, or is there any other option for this?
Just check in service launched by Alarm that current time early than your specific time
if (isTimeExpired) {
AlarmManager alarmManager=(AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pendingIntent);
}

Canceling alarms from AlarmManager

I am working on application which is get the alarm time from database every 2 AM and then at the specified time the activity shows up, my app works fine, but when I manually change the data/time of the device, previous alarms are triggered and I should prevent to this to happens.
Here is my PendingIntent:
Intent i = new Intent(mContext, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(G.context, _id, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Which each _id is unique and when I make a receiver for date changed event before my alarm cancel method fired previous alarms go off!
Can some body help to solve this problem?
Try this code:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);
For cancel the alarm:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Edit
Well you can look for the answer and modify as per your requirements:
How to stop Android AlarmManager when there is a manual system time change?
AlarmManager: how to schedule a daily alarm and deal with time changes
Intent intent1 = new Intent(G.context, DailyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context,
G.dateTimesetAlarm.getDayOfYear(), intent1, 0);
mAlarmManager.cancel(pendingIntent);
pendingIntent.cancel();

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 );

How to cancel AlarmManager using PendingIntents in different classes?

I am trying to use AlarmManager for doing some tasks upon an alarm time I set.
I know that I have to use same PendingIntent to make and cancel alarm.
But, the problem is that I am making the alarm in class "ClassA" and cancelling the alarm in class "ClassB" later on.
That's why I wonder if the intents to be broadcast in each class are same or not since they use "different contexts" as they are in different classes, I guess.
Can they be considered as same PendingIntent and Can I cancel the alarm?
If not, how can I make them same?
For making an alarm in class "ClassA" (that extends BroadcastReceiver):
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, myReceiver.class);
PendingIntent pender = PendingIntent.getBroadcast(context, codeNum, intent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pender);
For cancelling the alarm in class "ClassB" (that extends BaseAdapter):
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, myReceiver.class);
PendingIntent pilocal = PendingIntent.getBroadcast(context, codeNum, intent, 0);
pilocal.cancel();
alarm.cancel(pilocal);
You can retrieve the same kind of PendingIntent by providing same operation, same Intent action, data, categories, and components, and same flags.
Yes, you can cancel your alarm from another class if its still valid. Just make sure you pass the same parameters used, request code used to create the pending intent.

Canceling alarm manager when service has finished

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);

Categories

Resources