Android闹钟无法启动

时间:2016-01-04 14:25:10

标签: java android alarmmanager android-alarms

我想在一天中的某个特定时间设置闹钟,以便在那时工作。这是我的代码:

 //Create alarm manager
 AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 //Create pending intent & register it to your alarm notifier class
 Intent intent = new Intent(this, AlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

 //set timer you want alarm to work
 Calendar timeOff = Calendar.getInstance();
 timeOff.set(Calendar.HOUR_OF_DAY, myHour);
 timeOff.set(Calendar.MINUTE, myMinutes);
 timeOff.set(Calendar.SECOND, 0);

 //set that timer as a RTC Wakeup to alarm manager object
 alarmMgr.set(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), pendingIntent);

AlarmReceiver是一个内部类,位于同一个类中,用于表示此代码。这是AlarmReceiver的定义:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(BackgroundSensorService.this, "ALARM TRIGGERED", Toast.LENGTH_SHORT).show();
    }
}

我无法弄清楚为什么吐司没有显示出来。似乎AlarmReceiver永远不会得到任何意图,因此永远不会运行。

我做错了吗?

0 个答案:

没有答案