wts*_*g02 23
您已经确定了执行代码片段的时间(间隔),最好使用AlarmManager,因为它更节能.如果您的应用需要收听某种事件,那么服务就是您所需要的.
public static void registerAlarm(Context context) {
Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);
PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);
// We want the alarm to go off 3 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 3 * 1000;//start 3 seconds after first register.
// Schedule the alarm!
AlarmManager am = (AlarmManager) context
.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
600000, sender);//10min interval
}
Run Code Online (Sandbox Code Playgroud)