2017-01-26 1 views
0

내 앱에서 사용자가 설정 한 알람을 여러 번 원합니다. 그러나 다른 날짜와 시간으로 알람을 설정하면 마지막 설정 시간에만 응답합니다.Android에서 여러 번 알람 설정

이러한 내가 날짜 20-01-2017 시간 10.10 am, 20-01-2017 시간 10.20 am에 대한 알람, 그렇게 만 마지막에 선택한 날짜 & 시간에 대한 경보를 제공 10.30 am, 및 20-01-2017 시간을 설정하는 경우 등. 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로 경보 알림을 생성 할 수 없습니다. 그 전에 알림 취소 및 재정의를 의미합니다. PendingIntent.getBroadcast()이 호출 될 때마다 RQS_1의 고유 ID를 설정하십시오. 아마 개인 변수를 선언하고 그것을 증가시켜야합니다.

+0

나는 {private int RQS_1; RQS_1 = (int) System.currentTimeMillis();} 그러나 여전히 같은 문제. – rabiul