Start app at a specific time in Android - android

I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Following is the receiver BroadCast method
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
The alarm service class displays a notification.
/...Display notification.../

Related

Android cancelling pending intent not working

I am facing this really weird problem. I have made a working alarm system. But when I delete the alarm, it calls it.
AlarmBuddy.java (Working class to set original alarm)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DAY_OF_MONTH, d);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, repeat%60);
final Intent myIntent = new Intent(this.context, AlarmMiddle.class);
myIntent.putExtra("name", name);
myIntent.putExtra("id", repeat);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
super.onBackPressed();
PendingIntent pending_intent = PendingIntent.getBroadcast(AlarmBuddy.this, repeat, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
ListRecord.java THE FLAW
public Boolean cancelAlarm(int id){
Intent myIntent = new Intent();
PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT).cancel();
return false;
}
This function is to cancel the alarm. This has some problem to say this again:
The code can SET AND CALL an alarm. But when I delete it from a DIFFERENT class, it immediately calls the alarm. Maybe I am just a noob. Or maybe this is an unsolvable problem.
This is working for me at all in my application see link
Intent intent = new Intent(getBaseContext(), Your_Service.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), reqcode, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
To cancel the alarm, you need to use the same PendingIntent as you used to set it. You should also tell the AlarmManager to cancel the alarm as well as canceling the PendingIntent. Try this:
public Boolean cancelAlarm(int id) {
Intent myIntent = new Intent(context, AlarmMiddle.class);
PendingIntent pending_intent = PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_NO_CREATE);
if (pending_intent != null) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pending_intent);
pending_intent.cancel();
}
return false;
}

PendingIntent For Multiple Alarms

StartAlarmMethod();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 , pi);
I am using PendingIntents for multiple Alarms. How can i properly use TAGS, so i can later cancel only the Alarm that i don't want to use anymore?
Right now i'm seting Alarms with the code above. But if i set more than 1, using the code appears bellow i stop all upcoming Alarms. Instead of that i want to be able to identify somehow this PendingIntents and cancel only the ones that are not required.
CancelMethod();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
To support multiple alarms send a different request code each time you call it in the getBroadcast method. To cancel just send the same request code.
Below code you can loop it within for.
Note: replace i with any value and when cancelling pass the same value to cancel the particular alarm.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, alarmIntent, 0);
manager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent);
To cancel, send the requestcode which you want to cancel,
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, i, intent, 0);
alarmManager.cancel(sender);

Alarm not getting cancelled

I am setting the alarm in one activity, and want to cancel it in another class which is a service. Following is my code:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
And following is where i am deleting the alarm (where a is pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));

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.

Why my alarm is not being canceled

i have a service(AlarmService.java) where the alarm is being declared:
Intent intentAlarm = new Intent(this, TimeAlarm.class);
intentAlarm.putExtra("Notif_body", Notif_Body);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+5*1000, displayIntent);
and an activity(user_settings.java) from where I want to delete that alarm once a checkbox is unchecked:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(this,TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.cancel(displayIntent);
but it's not working my alarm is not being deleted why is that although the pendingintent is the same as it should be!!
intentAlarm.putExtra("Notif_body", Notif_Body);
Put the above line while creating the intentAlarm during cancelling the alarm or remove it from the time when you are setting the alarm. I guess this would work.
My problem was :
instead of adding:
if(bool_activate_reminder==true){
//call the alarm method;
}
I was doing:
if(bool_activate_reminder=true){//with one equal
//call the alarm method;
}
so it was always being accessed... when I fixed it and used the following code in the service:
Intent intentAlarm = new Intent(getApplication(), TimeAlarm.class);
//add the notification body to the intent:
intentAlarm.putExtra("Notif_body", Notif_Body);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set the alarm for particular time
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+20*1000, displayIntent);
and in the activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(getApplication(),TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(displayIntent);
it worked just fine..

Categories

Resources