代码如下:
public class BootReceiver extends BroadcastReceiver {
private PendingIntent mAlarmSender;
@Override
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)public void onReceive(Context context, Intent intent) {
// 在这里干你想干的事(启动一个Service,Activity等),本例是启动一个定时调度程序,每30分钟启动一个Service去更新数据
mAlarmSender = PendingIntent.getService(context, 0, new Intent(context,
RefreshDataService.class), 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) context
getSystemService(Activity.ALARM_SERVICE);
am.cancel(mAlarmSender);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
30 * 60 * 1000, mAlarmSender);
}
}
接下来,我们只需要在应用程序配置文件AndroidManifest.xml中注册这个Receiver来监听系统启动事件即可,如下所示:
view source
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/) print?
代码如下:
receiver Android:name=".service.BootReceiver"
intent-filter
!-- 系统启动完成后会调用--
action android:name="Android.intent.action.BOOT_COMPLETED"
/action
/intent-filter
/receiver