0

所以我一直在关注使用 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);
            }
        });

    }

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

@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();
}
4

1 回答 1

0

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

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

您可能需要替换thisthis.getApplicationContent().

于 2013-04-07T17:09:33.133 回答