2017-01-26 59 views
0

在我的應用程序中,我需要多次由用戶設置的鬧鐘。 但是,如果我將鬧鐘設置爲不同的日期和時間,它只會響應最後一次設置時間。在Android中多次設置鬧鐘

比如,如果我設置了日期20-01-2017時間10.10 am20-01-2017時間10.20 am報警,20-01-2017時間10.30 am, 等只給出了最後選定的日期&時間報警。 但是,在20-01-2017時間10.10 am,20-01-2017時間10.20 am或其他選定時間沒有發出警報。

如何在所有選定時間發出警報? 這裏是我的代碼:

public void setDate(View view) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
} 

public void setTime(View view) { 

    DialogFragment newFragment = new TimePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "timePicker"); 
} 


public void setAlarm(View view) { 
    Calendar calNow = Calendar.getInstance(); 
    Calendar calSet = (Calendar) calNow.clone(); 
    calSet.set(Calendar.HOUR_OF_DAY, alarmHour); 
    calSet.set(Calendar.MINUTE, alarmMin); 
    calSet.set(Calendar.DAY_OF_MONTH, alarmDay); 
    calSet.set(Calendar.YEAR, alarmYear); 
    calSet.set(Calendar.MONTH, alarmMonth); 

    setAlarmN(calSet); 
} 

private void setAlarmN(Calendar targetCal) { 

makeText(this, "Alarm is set at" + targetCal.getTime(), 
     LENGTH_LONG).show(); 
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(
     getBaseContext(), RQS_1, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 
     pendingIntent); 


} 

//date picker fragment 
public static class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

     // Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     // Do something with the date chosen by the user 
     alarmDay = day; 
     alarmYear = year; 
     alarmMonth = month; 
    } 
} 

//Time picker fragment 
public static class TimePickerFragment extends DialogFragment 
     implements TimePickerDialog.OnTimeSetListener { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 
     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 

     // Create a new instance of TimePickerDialog and return it 
     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // Do something with the time chosen by the user 
     alarmHour = hourOfDay; 
     alarmMin = minute; 
    } 
} 
+0

您需要使用不同的意圖ID多次激發它。使用相同的ID意味着您取消之前的計劃,並設置新的計劃。 –

+0

我不清楚這個可以給我一個例子我如何使用不同的意圖ID來解決這個問題? – rabiul

+0

'PendingIntent.getBroadcast'有4個參數,第二個參數是請求ID。你使用'RQS_1'。嘗試爲每個鬧鐘使用不同的ID。 –

回答

0

問題是在你的void setAlarmN()方法。您無法使用相同的ID生成警報通知。這意味着之前的通知會取消並覆蓋。嘗試爲RQS_1設置唯一的ID,每次調用PendingIntent.getBroadcast()。可能你必須聲明私有變量並增加它。

+0

我已經使用{private int RQS_1; RQS_1 =(int)System.currentTimeMillis();}但仍然是同樣的問題。 – rabiul