在AlarmManager未触发时设置警报

时间:2013-04-07 17:03:02

标签: android alarmmanager

所以我一直在关注使用AlarmManager设置闹钟的教程,我试图弄清楚我做错了什么。我是否需要覆盖onReceive方法?

我正在使用DatePicker和TimePicker小部件并从中获取数据和用户的输入。我设置闹钟的主要课程在这里:

public void initControls() {
        timePicker = (TimePicker)findViewById(R.id.timePicker);
        datePicker = (DatePicker)findViewById(R.id.datePicker);
        setAlarm = (Button)findViewById(R.id.setAlarm);
        myCal = Calendar.getInstance();


        setAlarm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

                myCal.set(Calendar.YEAR, datePicker.getYear());
                myCal.set(Calendar.MONTH, datePicker.getMonth());
                myCal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                myCal.set(Calendar.HOUR, timePicker.getCurrentHour());
                myCal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                myCal.set(Calendar.SECOND, 0);

                Intent triggered = new Intent("alarms.DisplayNotification");
                triggered.putExtra("NotificationId", 1);

                PendingIntent displayIntent = PendingIntent.getActivity(
                        getBaseContext(), 0, triggered, 0);

                alarmManager.set(AlarmManager.RTC_WAKEUP, 
                        myCal.getTimeInMillis(), displayIntent);
            }
        });

    }

我正在使用的另一个类是使用notificationBar并显示已触发警报。

@Override
public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);


    int notifID = getIntent().getExtras().getInt("NotificationId");


    Intent i = new Intent("com.example.mt_study.Main_screen");
    i.putExtra("NotificationId", notifID);

    PendingIntent displayAlarm = PendingIntent.getActivity(this, 0, i, 0);

    NotificationManager nm = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
        Notification notif = new Notification(
            R.drawable.book, 
            "Time's up!",
            System.currentTimeMillis());

        CharSequence from = "AlarmManager - Thats all";
        CharSequence message = "Alarm DONE";        
        notif.setLatestEventInfo(this, from, message, displayAlarm);

        //---100ms delay, vibrate for 250ms, pause for 100 ms and
        // then vibrate for 500ms---
        notif.vibrate = new long[] { 100, 250, 100, 500};        
        nm.notify(notifID, notif);
        //---destroy the activity---
        finish();
}

1 个答案:

答案 0 :(得分:0)

警报将尝试创建活动“alarms.DisplayNotification”。我怀疑这不会引发你的课。您可能需要一个不同的警报意图:

Intent triggered = new Intent(this, MainActivity.class);

您可能需要将this替换为this.getApplicationContent()