2

알람 관리자와 브로드 캐스트 수신기를 사용하는 알람 시계 앱이 있습니다. 앱은 하나의 활동이며 4 개의 조각입니다. 알람이 울리면 onReceive 메소드는 인 텐트를 주 활동으로 보내고 주 활동은 onIewIntent 메소드에서이 인 텐트를 수신 한 다음 올바른 프래그먼트로 이동합니다. 앱이 종료 된 후 알람이 울리는 경우를 제외하고 모든 것이 잘 작동합니다.onNewIntent가 다시 시작될 때 호출되지 않음

일단 앱을 파괴하면 알람이 계속 울리며 브로드 캐스트 리시버의 의도가 실행되지만 onNewIntent 메소드는 의도를 파악하여 앱을 올바른 조각으로 이동시킵니다. 여기

는 알람이 때 응용 프로그램을 호출 할 때 호출 점점되지 내 주요 활동 내 onNewIntent 방법의 주요 활동 여기

Intent alarmIntent = new Intent(context, ClockActivity.class); 
       alarmIntent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
       alarmIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       alarmIntent.putExtra("Alarm Name", receivedAlarm.getmName()); 
       context.startActivity(alarmIntent); 

로 이동 방송 수신기 클래스에 텐트입니다 닫혀있다.

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

    PhraseFragment phraseFragment = new PhraseFragment(); 

    String activeName = intent.getStringExtra("Alarm Name"); 

    Bundle args = new Bundle(); 
    args.putString("activeName", activeName); 
    phraseFragment.setArguments(args); 

    getFragmentManager().beginTransaction() 
      .replace(R.id.container, phraseFragment) 
      .addToBackStack("phrase") 
      .commit(); 

} 
+0

참조 : http://stackoverflow.com/questions/8800006/onnewintent-is-not-called-in-android-tabs – grebulon

답변

0

다소 늦었지만 누군가에게 도움이 될 수 있습니다.

위에서 볼 수 있듯이 onNewIntent는 백그라운드에서 활동이 열릴 때 호출됩니다. 배경에서 실행되지 않는 활동에 인 텐트를 보낼 때 onResume()에서 getIntent를 통해 검색 할 수 있습니다.

코드가 다음과 같이 변경됩니다. 당신은 당신이 onResume에서 수신 한 의도는() 당신이 필요로하는 데이터가 포함되어 있는지 확인하기 위해 필요한이 경우

@Override 
protected void onResume() { 
    super.onResume(); 

    Intent intent = getIntent(); 
    String activeName = intent.getStringExtra("Alarm Name"); 

    if (activeName != null){ 
     PhraseFragment phraseFragment = new PhraseFragment(); 

     Bundle args = new Bundle(); 
     args.putString("activeName", activeName); 
     phraseFragment.setArguments(args); 

     getFragmentManager().beginTransaction() 
       .replace(R.id.container, phraseFragment) 
       .addToBackStack("phrase") 
       .commit(); 
    } 
} 

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

.

문서에서 이에 대한 참조를 찾지 못했습니다. 그 결론은 실험으로 얻었습니다.

관련 문제