android自动运行服务,Android开机自启动服务的实现
public class BootReceiver extends BroadcastReceiver {private PendingIntent mAlarmSender;@Overridepublic void onReceive(Context context, Intent intent) {// 在这里干你想干的事(启动一个Service,Activity等),本例是启动一个定时调度程
public class BootReceiver extends BroadcastReceiver {
private PendingIntent mAlarmSender;
@Override
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);
}
}
更多推荐
所有评论(0)