2013-04-07 135 views
0

所以我一直在使用AlarmManager来设置闹钟的教程,我试图弄清楚我做错了什么。我需要重写onReceive方法吗?使用AlarmManager设置闹钟不会触发

我使用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并显示Alarm已触发。

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

回答

0

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

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

您可能需要this.getApplicationContent()更换this

+0

另一个选择是让第二个活动在清单中实现一个意图过滤器,它拦截“alarms.DisplayNotification” – fedepaol 2013-04-07 17:11:39

+0

感谢您的快速响应!我在看教程时觉得这很奇怪,但是我改变了它,似乎没有效果。我现在提前一分钟设置闹钟。 – cj1098 2013-04-07 17:14:48

+0

我已经缩小到displayNotifications类。我想我可以从这里处理它。谢谢您的帮助 :) – cj1098 2013-04-07 17:21:09