2015-09-16 73 views
1

我需要每天重複報警。這就是我一次觸發鬧鐘的方式。如何在Android中重複鬧鐘?

public void clicker (View view) 
{ 

    int hour = calendar.get(Calendar.HOUR_OF_DAY); 
    int minute = calendar.get(Calendar.MINUTE); 
    TimePickerDialog mTimePicker; 
    mTimePicker = new TimePickerDialog(AddAlarm.this,new TimePickerDialog.OnTimeSetListener() 
    { 
       @Override 
       public void onTimeSet(TimePicker timePicker, 
             int selectedHour, int selectedMinute) 
       { 


        calendar.set(Calendar.HOUR_OF_DAY, selectedHour); 
        calendar.set(Calendar.MINUTE, selectedMinute); 

        int c_hour,c_min; 
        String format; 
        c_hour=selectedHour; 
        c_min=selectedMinute; 

        if (c_hour == 0) { 
         c_hour += 12; 
         format = "AM"; 
        } else if (c_hour == 12) { 
         format = "PM"; 
        } else if (c_hour > 12) { 
         c_hour -= 12; 
         format = "PM"; 
        } else { 
         format = "AM"; 
        } 

        String formatTime = String.format("%02d : %02d %s", c_hour, c_min, format); 

        TextView dimple = (TextView)findViewById(R.id.timeText); 
        dimple.setText(formatTime); 
        setAlarm(calendar); 

       } 

    }, hour, minute, false); 
    mTimePicker.setTitle("Select Time"); 
    mTimePicker.show(); 



} 

private void setAlarm(Calendar targetCal) 
{ 
    Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class); 
    alarmintent.putExtra("ALARM_NO", tempx); 
    alarmintent.setAction("my.action.string"); 
    PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender); 


} 

現在應該如何更改我的代碼來每天重複觸發警報?我怎樣才能殺死鬧鐘呢?謝謝,

回答

0

使用AlarmManager.setRepeating()方法,而不是AlarmManager.setExact()

如:

// Set the alarm to start at 8:30 a.m. 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 8); 
calendar.set(Calendar.MINUTE, 30); 

// setRepeating() lets you specify a precise custom interval--in this case, 
// 20 minutes. 
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
     1000 * 60 * 20, alarmIntent); 

Here你可以找到如何安排重複報警,如何更多的細節取消它。

0

要報警重複使用alarmManager.setRepeating()
如:

Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class); 

     pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0); 
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(System.currentTimeMillis()); 
       calendar.set(Calendar.HOUR_OF_DAY, 10); 
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
         1000 * 60 * 60 * 12, pendingIntent); // format : nanosec*sec*min*hr 
0

如果你想喚醒手機不能使用alarmManager.setRepeating(),因爲它不會wake up從API 19.手機,您就必須重新創建報警每次。 這是我如何創建事件:

PendingIntent i = PendingIntent.getBroadcast(this, 1, alarmIntent, 0); 
TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 

       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
        // set the TexView's text. 
        ((TextView) v).setText(String.format("%02d:%02d", hourOfDay, minute)); 
        // cancel previous alarm 
        manager.cancel(i); 
        // set alarm if the time is not 00:00 
        if(hourOfDay != 0 || minute != 0) { 
         Calendar calendar = Calendar.getInstance(); 
         calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
         calendar.set(Calendar.MINUTE, minute); 
         calendar.set(Calendar.SECOND, 0); 
         // if the time is before now then add one day to it 
         if(calendar.getTimeInMillis()<System.currentTimeMillis()) 
          calendar.setTimeInMillis(calendar.getTimeInMillis()+86400000); 
         manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), i); 
        } 
       } 
      }, 12, 0, true); 
      tp.setTitle(getResources().getString(R.string.time_picker_title)); 
      tp.show(); 

,這是我再怎麼每次都設置它,我測試過它,它工作得很好。

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
//Setting up the next notification 
     PendingIntent alarmPIntent = PendingIntent.getBroadcast(context, 0, 
           new Intent(context, AlarmReceiver.class), 0); 
     AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     int interval = 86400000; //one day 
     Calendar calendar = Calendar.getInstance(); 
     manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + interval, alarmPIntent); 
     Log.i(TAG, "Next alarm set to one day after now"); 
    } 
} 

你可以做任何你想要的,當你收到報警,只是你也必須添加接收器清單文件的aplication標籤:<receiver android:name="hu.bendaf.vivago.datatransmission.communication.AlarmReceiver"/> 就是這樣。