1
我有一个服务,我想每天运行,所以检查一些东西在我的数据库,如果需要创建一个通知。 为了每天运行我的服务,我使用了一个alarmManager,它第一次工作正常,但是一旦启动,我的服务进入一个无限循环,我知道这是因为alarmManager,因为它只是进入循环当警报管理员启动时。这里是我的服务代码:Android alarmManager进入循环
public class MyService extends Service {
...
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
checkMechanicUseer();
this.stopSelf();
return START_NOT_STICKY;
}
private void checkMechanicUseer() {
...
}
@Override
public void onDestroy() {
super.onDestroy();
final SharedPreferences settings = getSharedPreferences("MYSETTINGS",0);
int time = settings.getInt("time",9);
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, 9);
calSet.set(Calendar.MINUTE, 0);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0); // I want it to trigger at 09:00 am each day
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.setRepeating(
alarm.RTC_WAKEUP,
calSet.getTimeInMillis() ,(1000 * 60),
PendingIntent.getService(this, 0, new Intent(this, MyService.class), 0)
); // I set the (1000 * 60) so I can check it with 1 min interval, so I wont need to wait one day for it ... of course I need to change it to (1000 * 60 * 60 * 24)
Toast.makeText(MyService.this, "Service destroyed",Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
我想我需要取消我的闹钟一些地方,然后设置另一个。但不知道如何或在哪里做
仍然有问题,如果我有alarm.set
改变alarm.setRepeat
如下:
alarm.set(
alarm.RTC_WAKEUP,
calSet.getTimeInMillis() + (1000 * 60),
PendingIntent.getService(this, 0, new Intent(this, MyService.class), 0)
);
这是问题,谢谢你。 但让我说,如果我将间隔设置为(1000 * 60 * 60 * 24),并设置校准小时,分钟,秒和毫秒,第二个代码(只有'alarm.set'将起作用won举起了@Jivy发现的问题 – 2014-12-02 12:29:59
这对我来说也是一个问题,我的函数检查过去是否存在逻辑错误,并且让AlarmManager进入了一个循环, 4秒钟,你可以看到报警计划使用'adb shell dumpsys alarm'的时间,并使用'-a'标志为你的应用程序名称进行grep以获取后面的行。 – 2017-06-24 02:02:09