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
}
}