用Alrammanager 在里面发送广播 广播启动service service里面更新时间..
- public class AlarmManagerUtil {
-
- public static AlarmManager getInstance(Context context) {
- return (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
- }
-
- public static void sendBroadCast(Context context) {
- Intent intent = new Intent(context , AlarmManagerBroadcast.class);
- intent.setAction("com.alarm.repeat");
- PendingIntent pen = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
-
- long firstTime = SystemClock.elapsedRealtime();
- AlarmManager alarmManager= getInstance(context);
- alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 10*1000, pen);
- }
-
- public static void cancelBroadCast(Context context) {
- Log.i("yh", "cancelAlram");
-
- Intent intent = new Intent(context , AlarmManagerBroadcast.class);
- intent.setAction("com.alarm.repeat");
- PendingIntent pen = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager alarmManager= getInstance(context);
- alarmManager.cancel(pen);
- }
- }
- //这个是Alrammanager的
- public class AlarmManagerBroadcast extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if(("com.alarm.repeat").equals(action)) {
- Log.i("yh", "repeat");
- Intent serviceIntent = new Intent(context, PushService.class);
- context.startService(serviceIntent);
- }
- if(("com.alarm.cancel").equals(action)) {
- Log.i("yh", "cancel");
- Intent serviceIntent = new Intent(context, PushService.class);
- context.stopService(serviceIntent);
- }
- }
- }
复制代码
你看看能不能直接在BroadcastReceiver更新时间 |