我对ApiDemo中的AlarmController.java做了一点修改,所以我希望通过使用AlarmManager.RTC,当手机处于睡眠状态时,闹钟不会响起。
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, //AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000, sender);接收器代码如下:
public class RepeatingAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("DEBUG", "In RepeatingAlarm.onReceive, intent=" + intent);
Toast.makeText(context, R.string.repeating_received, Toast.LENGTH_SHORT).show();
}
}我运行了修改后的应用程序,但在手机进入睡眠状态后(屏幕是黑色的),我仍然看到许多日志消息,如下所示:
D/DEBUG ( 1390):In cmp=com.example.android.apis/.app.RepeatingAlarm,RepeatingAlarm.onReceive { intent=Intent flg=0x4 (has extras) }
这意味着标志AlarmManager.RTC不起作用。有人能告诉我为什么吗?
谢谢。
发布于 2010-03-03 09:20:01
由于您使用RTC来获取警报开始时间,因此我认为您需要使用ELAPSED_REALTIME标志,而不是RTC标志。
我的猜测是,警报管理器认为它错过了大量警报,因为您使用的是RTC标志,这意味着警报管理器希望您发送一个自1970年1月1日以来以毫秒为单位的时间值,但实际上您发送的是设备启动后经过的毫秒数,这将是一个小得多的数字。
如果使用RTC标志,则需要使用System.currentTimeMillis()或从Java Date或Calendar对象获取以毫秒为单位的时间。如果使用ELAPSED_REALTIME标志,则需要使用SystemClock.elapsedRealtime()。
https://stackoverflow.com/questions/2368098
复制相似问题