내 활동에서 사용자가 제공 한 음식 목록을 관리하고 만료시기에 따라 알람을 설정하고 있습니다. 그러나 어떤 이유로 인해 onRecieve에 전달 된 의도는 내가 그것이되어야한다고 생각한 것이 아닙니다.Android getExtras/예상과 다른 의도
예를 들어 사용자가 만기 날짜가 5/8 인 버터를 입력하고 만기가 6/4 인 버터를 입력 한 다음 저장 데이터를 누릅니다. 각 항목에 대해이 메소드가 호출됩니다. 이 메서드를 호출 할 때마다 putExtra를 사용하여 만료 날짜를 저장합니다.
public void setOneTimeAlarm(int daysAfterSet) {
//declare intent using class that will handle alarm
Intent intent = new Intent(this, FoodExpAlarm.class);
//retrieve pending intent for broadcast, flag one shot means will only set once
intent.putExtra("expDate", expDate);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
//params: specify to use system clock use RTC_WAKEUP to wakeup phone for notification,
//time to wait, intent
alarmMan.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (daysAfterSet * AlarmManager.INTERVAL_DAY), pendingIntent);
}
이 그들이 만료 및 (한 달에 떨어져 만료으로부터) 버터 경보 없음에 근접하기 때문에 하나의 알람이 열매를 설정할 수됩니다. 그런 다음 foodExpAlarm 클래스가 한 번 호출되고 열매에 대한 알림이 올바르게 표시됩니다. 그러나 문제는 다음 번 음식을 입력 할 때 발생합니다. 다음으로 사용자는 베이컨 만료 5/8 및 바나나 5/15를 입력합니다. 이제 setOneTimeAlarm이 다시 두 번 호출되고 (각각에 대해 한 번씩) foodExpAlarm이 베이컨에 대해 한 번 다시 호출됩니다.
그러나 foodExpAlarm에서 전달되는 추가 항목은 베이컨이 아니라 버터에서 가져옵니다. foodExpAlarm은 시간이 경과 한 후에 호출되어야하고 예약 된 시간에 해당하는 보류 인 텐트와 함께 호출되어야하기 때문에 이것은 나에게 이해가되지 않습니다. 그러나 그 의도 (또는 적어도 최소한의 추가 사항)는 모든 식품이 추가 된 순서에만 해당하는 것으로 보이는 경우는 아닙니다.
실행 요약 :
- 열매를 5/8
- 을 추가 버터 6/4
- 저장
- 올바르게 5/8
- 바나나를 추가 베이컨 추가
- 베리 알림을 표시 5/15
- 저장
- 버터를 잘못 표시했습니다.
내 질문은 왜 버터가 아닌 베이컨을위한 것입니까? 의도에 대한 나의 이해가 잘못 되었습니까? 이 문제를 어떻게 해결할 수 있습니까?
foodExpAlarm :
public void onReceive(Context context, Intent intent) {
Bundle exp = intent.getExtras(); //This gets the extras from butter, not bacon like I want
Object temp = exp.get("expDate");
Enter_Foods.expDate = new DateTime(temp);
intent.getExtras();
int id = (int) (Enter_Foods.expDate).getMillis();
....
notificationMan = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
//i think this gets pending intent from the alarm that called this method
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context,MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
//creates notification object/icon, and set text to flow across top bar
Notification notif = new Notification(R.drawable.ic_launcher,
"Food expiring soon", System.currentTimeMillis());
//specify what to display when notification is shown
notif.setLatestEventInfo(context, from, message, contentIntent);
//use nofication manager to send message to phone, will update if same id
notificationMan.notify(id, notif);
}
Activity.setIntent()와 관련이 있다고 생각합니다. 나는 당신이 onNewIntent를 오버라이드하고 새로운 인 텐트를 사용하기 위해 actitiy을 업데이트해야한다고 생각한다. – j2emanue
onRecieve에 전달되는 매개 변수에 영향을 줍니까? – farnett
알림을 설정하는 데 사용하는 코드를 게시하십시오. –