0

사용자가 기억해야 할 작업 목록을 작성할 수있는 todo 응용 프로그램을 만들고 있습니다. 목록에 모든 인스턴스를 만들 때 엔트리 기한에 도달하면 경보가 울리도록 설정했습니다. 알람 수신자는 마감일에 도달했음을 사용자에게 알려주는 알림을 생성합니다.android에서 여러 개의 알람을 생성하는 데 문제가 있습니다.

내가 겪고있는 문제는 알림에 의도적으로 전달하는 여분의 문자열이 제대로 업데이트되지 않는다는 것입니다. 첫 번째 경보는 잘 작동하지만 첫 번째 항목 다음에 만드는 모든 항목은 첫 번째 항목과 동일한 추가 문자열을 가져옵니다.

다음과 같이 알람을 설정하기위한 내 코드는 다음과 같습니다

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

Intent intent = new Intent(context, DeadlineActivator.class); 
intent.setAction("todo" + System.currentTimeMillis()); 
intent.putExtra("todoname", name); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
     (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
am.set(AlarmManager.RTC_WAKEUP, deadline.getTime(), pendingIntent); 
는 어떤 방식으로 helpfull하지만 항목의 이름은 알람

onRecieve 방법 고유 한 경우

는 몰라 디버깅, 나는 알람에 대한 의도를 만들 때 사용하는 이름 var에 올바른 값이 있는지 볼 때이

public void onReceive(Context context, Intent intent) 
{ 
    String todoName = intent.getStringExtra(ToDoItem.TODONAME); 

    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent onClickIntent = new Intent(context, WatchToDo.class); 
    onClickIntent.putExtra(Auth.TARGET_TODO, todoName); 
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0); 
    String body = "Deadline of " + todoName + " has been reached"; 
    String title = "Deadline reached"; 
    Notification n = new Notification.Builder(context) 
    .setContentTitle(title) 
    .setContentText(body) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentIntent(pIntent) 
    .setAutoCancel(true) 
    .build(); 
    nm.notify(NOTIFICATION_ID, n); 
} 

것 같습니다. 그러나 onRecieve의 todoName var은 이전 값을 유지합니다.

답변

0

이것은 일반적인 문제입니다. PendingIntent의 작동 방식을 실제로 이해해야합니다.

PendingIntent pIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0); 

안드로이드 onClickIntent를 시작하는 데 사용할 수있는 "토큰을"반환 문제는 당신이 호출 할 때이다. 안드로이드는 내부적으로 관리되는 목록에이 Intent을 저장하고 토큰을 할당합니다.

는 다음 번 전화 :

PendingIntent pIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0); 

안드로이드는이 하나와 일치 하나가 있는지 PendingIntent의 자사의 목록에서 찾습니다. Intent 개체 의 비교에는 추가이 포함되어 있지 않으므로 이전 onClickIntent을 찾고 처음과 완전히 동일한 토큰을 반환합니다. 토큰을 Notification에 전달하고 NotificationActivity으로 시작하면 첫 번째 세트를 가져옵니다.

PendingIntent.getActivity()에 대한 모든 호출이 고유 한 토큰를 반환하는지 확인해야이 문제를 해결하려면 안드로이드 올바른 엑스트라와 onClickIntent 각각 별도의 인스턴스를 저장된다. 이렇게하려면 두 번째 매개 변수를 고유 한 값으로 설정하면됩니다 (예 :

int uniqueValue = (int)currentTimeMillis(); 
PendingIntent pIntent = PendingIntent.getActivity(context, uniqueValue, onClickIntent, 0); 
).
관련 문제