How to restart Alarm after phone Reboot? - android

In my project I'm using AlarmManager to start a Service once a day. It's working fine. But it stops working when the phone reboot are restarts.
I searched through web and they suggested to start a BroadcastReceiver to listen RECEIVE_BOOT_COMPLETED action and restart the Alarm.
I don't know how to do that.
Please suggest me an easy way to reset the alarm. I've posted my code here.
My Code to start Alarm
Intent start_alarm=new Intent(MainPage.this,MailService.class);
PendingIntent pi=PendingIntent.getService(MainPage.this, 100, start_alarm, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
My AlarmService
#Override
public void onStart(Intent intent,int startId){
super.onStart(intent,startId);
//* My Code for sending Mail *//
}
Manifest Registeration of Service
<service android:name=".MailService"/>

Simply create a broadcast receiver and register it on manifest with intent action_reboot, then start the alarm in onReceive method
this is how to register receiver
<receiver android:name="com.packagename.RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
this is the receiver
public class RebootReceiver extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
//start your alarm
}
}

Related

How do I make alarmmanager execute after my application has been closed?

I want to make my alarmmanager to rings although after my application closed.
But now my code don`t call broadcastreceiver after my application closed.
I reg my broadcastreceiver in my manifest.
This is code that setting alarmmanager
final AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent=new Intent(setTimeActivity.this,autoCheckReceiver.class);
intent.setAction("com.dayo.selfcheck.autoCheckReceiver");
final PendingIntent pendingIntent=PendingIntent.getBroadcast(setTimeActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
calendar.set(Calendar.MINUTE, timePicker.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And this code is my broadcastreceiver.
public class autoCheckReceiver extends BroadcastReceiver {
private String TAG="autoCR";
#Override
public void onReceive(Context context, Intent it) {
Log.d(TAG,"asdf");
Intent i = new Intent();
i.setClassName("com.dayo.selfcheck", "com.dayo.selfcheck.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
this is my manifest file.
<receiver
android:name=".autoCheckReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.dayo.selfcheck.autoCheckReceiver"></action>
</intent-filter>
</receiver>
thanks!
You MUST specify android:exported="true" on your <receiver> declaration in order for AlarmManager to launch your BroadcastReceiver.
You don't need to call cancel() on the AlarmManager to cancel the Intent before you schedule it, as AlarmManager automatically does this.
You do not need to set the ACTION in the Intent for the broadcast, since you are using an explicit Intent (you have specified the exact component to be used).
You also do not need to provide the <intent-filter> in your <receiver> declaration because you are using an explicit broadcast.

AlarmManager is not triggered after complete bootup

I've used an alarmManager to send daily notification. Since alarmManager stops after restarting phone so I've created a BroadcastReceiver to trigger on BOOT_COMPLETE, still no success.
Even not getting toast.
BroadcastRreceiver class
package com.aman.dailynoti;
import...
public class BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction()).equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "broadcast", Toast.LENGTH_SHORT).show();
SharedPreferences mpreferences=context.getSharedPreferences("myPreferences",MODE_PRIVATE);
int h=mpreferences.getInt("hour",14);
int m=mpreferences.getInt("minute", 30);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
calendar.set(Calendar.SECOND,00);
Intent notiIntent = new Intent(context, Notification_Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...>
<receiver
android:name="com.aman.dailynoti.BReceiver"
android:enabled="true"
android:label="breceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Dont quote me on this but I believe android only gives a small window of time for your applications to do what they need to do on boot. If you want the user to receive notifications when not using the app look into push notifications.
you should use also intent filter android.intent.action.QUICKBOOT_POWERON to recieve after restart

Alarm manager trigger every time app running

I want to create a repeating alarm from AlarmManager which is triggered at 21:00 every day to show a notification. So i create a service and declare that in manifest, in the service class i wrote a method for schedule repeating alarms.
public static void setRecurringAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NotificationService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.cancel(pi);
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 21);
updateTime.set(Calendar.MINUTE, 00);
updateTime.set(Calendar.SECOND, 00);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
}
(For test i set the delay INTERVAL_FIFTEEN_MINUTES).
I call this method at onResume() of my splash screen activity and in a BroadcastReceiver that is set up to receive BOOT_COMPLETED.
#Override
protected void onResume() {
super.onResume();
NotificationService.setRecurringAlarm(this);
}
this is my BroadcastReceicer :
public class NotificationBootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
NotificationService.setRecurringAlarm(context);
}
}
and this is my manifest :
<receiver android:name=".notification.NotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".notification.NotificationService" />
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
the problem is every time i open the app and splashscreen running the alarm triggered and notification will be shown.

alarmManager getting stopped when app is completely closed

I have a counter that needs to be reset at midnight every day as well as some notification that needs to be displayed whether the app is closed or not.
For that I have an alarmManager that is set up:
public void scheduleAlarm()
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_YEAR, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 01);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(getApplicationContext(), 1, intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
}
It is registered in the android manifest as follow:
<receiver android:name="com.whitepebblesplus.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</receiver>
and I have in the alarmReceiver class the following
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
My problem is that whenever the app is closed the alarm won't go off and so the counter won't be reseted, not until the app starts again, which is really not what I want.
Anyone of you has an idea on how to make the alarm goes off whether the app is running or not?
thanks

Broadcast receiver not called

I know this a basic problem but it is still driving me crazy. I am setting a repeating alarm but the receiver is never called.
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
Log.i("calendar",calendar.getTimeInMillis() + "");
Toast.makeText(NewSchedule.this, "repeating_scheduled", Toast.LENGTH_SHORT).show();
public class RepeatingAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
I am testing on my phone. The calendar log shows the exact time. I never get the Toast in the receiver class.
Reference : Android Alarm Manager with broadcast receiver
Intent sender = new Intent("WhatEverYouWant");
PendingIntent senderPIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, senderPIntent);
// In Manifest.xml file
<receiver android:name="com.package.YourOnReceiver">
<intent-filter>
<action android:name="WhatEverYouWant" />
</intent-filter>
</receiver>
Actually it turned out my code was good. Somehow the alarm was up and running and thus for some unknown reason (at least to me) the recevier could not be called. I figured it out when I created a new project and tested that that receiver was working fine. I also had to stop that alarm. Then I went back to my original project and started the same alarm without changing any lines and it was working fine. Has anyone experienced this?

Categories

Resources