ProgramingTip

getIntent () Extras 항상 NULL

bestdevel 2020. 10. 20. 07:53
반응형

getIntent () Extras 항상 NULL


다음과 같은 사용자 지정 알림을 표시하는 간단한 Android 앱을 작성했습니다.

Context context = getApplicationContext();          
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());  
Intent notificationIntent = new Intent( context,  this.getClass()); 
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE"); 
PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);               
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar);
notification.contentIntent = pendingIntent;

notification.contentView.setTextViewText(R.id.notifypb_status_text, text);
notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);

manager.notify(104, notification);

이 코드는 내 응용 프로그램에서 한 번만 호출 완료 진행률 표시가 모두 줄과 함께 알림이 표시됩니다.

이제 사용자 가이 알림을 클릭하면 내 애플리케이션이 onResume이벤트를 처리합니다 .

public void onResume()
{
    super.onResume();
    // TODO: Extras è SEMPRE NULL!!! impossibile!
    Intent callingintent = getIntent(); 
    Bundle extras = callingintent.getExtras();

그러나 실제로는 항상 NULL입니다!

나는 다음의 조합을 시도했습니다.

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");

또는

Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

그러나 getIntent (). getExtras ()는 항상 NULL을 반환합니다.


이것이 시나리오입니다.
방법은 getIntent()시작 활동보다 FIRST 인 텐트를 반환합니다.

따라서 활동이 CLOSED (종료 됨)이고 사용자가 알림을 클릭하면 활동의 새 인스턴스가 실행 getIntent()되고 예상 대로 작동합니다 (Extras는 아닙니다 null ).

그러나 활동이 "휴면 중"(백그라운드에 있음)이고 사용자가 알림을 클릭하면 항상 알림 getIntent()인 텐트가 아닌 활동을 첫 번째 텐트 인 텐트를 반환합니다.

애플리케이션이 실행되는 동안 알림을 제공합니다.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

그런 다음 onNewIntent(Intent newintent).

따라서 응용 프로그램이 처음 실행될 getIntent()때 사용할 수있는 응용 프로그램이 처음 실행될 때 다시 시작될 때 onNewIntent작동합니다.


Resume () 메서드 위에이 코드를 작성하십시오. 이것이 전부입니다. 이 의도를 새로 고칩니다.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

문제 : 보류중인 인 텐트에 대해 요청 코드를 전송하고 있습니다. 그것을 변경하십시오.

솔루션 : 전역 변수 int UNIQUE_INT_PER_CALL = 0을 설정하고 같이 pendingIntent 호출을 생성 할 때.

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.

활동이 이미 실행중인 것 같을 지정해야한다고 생각합니다 FLAG_UPDATE_CURRENT. 않으면 오는가 getIntent()호출이 이전 활동을 반환합니다. 이 답변을 참조하십시오 .


공유 환경 설정을 전달하고 지속적인 키 / 값 쌍을 검색하십시오.

참고 URL : https://stackoverflow.com/questions/6352281/getintent-extras-always-null

반응형