android定期重复闹钟无法正常工作

时间:2014-03-21 07:32:10

标签: android alarm android-alarms periodic-task

我试图每隔30分钟设置一次警报,但我不认为它有效。我使用下面的代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MyActivity.this, Alarm.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(MyActivity.this, 0, intent, 0);

long selectedTimeMiliseconds = (long) (TimeUnit.MINUTES.toMillis(30));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);

如何在方法setInexactRepeating中设置第二个参数?使用setRepeating方法更好吗?

2 个答案:

答案 0 :(得分:2)

不精确是一种节省电池的方法,但它是......不精确的。

AlarmManager有问题,如果设备处于睡眠模式,意图无法启动。你应该使用WakefulBroadCast。

https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

答案 1 :(得分:0)

试试这个..

alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), selectedTimeMiliseconds, alarmIntent);