在我的项目中,我使用AlarmManager
每天启动一次Service
。它工作得很好。但它停止工作时,手机重启是重启。我通过网络搜索,他们建议启动一个BroadcastReceiver
来听RECEIVE_BOOT_COMPLETED
的动作,并重新启动Alarm
。我不知道该怎么做。请给我一个复位闹钟的简单方法。我把我的代码贴在这里。
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);
我AlarmService @Override
public void onStart(Intent intent,int startId){
super.onStart(intent,startId);
//* My Code for sending Mail *//
}
服务舱单注册
<service android:name=".MailService"/>
只需创建一个广播接收器,并在manifest中用intent action_reboot注册它,然后在onReceive方法中启动警报
这是如何注册接收者
<receiver android:name="com.packagename.RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
这是接收者
public class RebootReceiver extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
//start your alarm
}
}