2014-04-06 5 views
1

비슷한 질문이 있지만 해결책이 없습니다. 브로드 캐스트 리시버가 SMS를 가로 채고 SMS 텍스트를 다른 활동으로 전달했습니다. 다른 활동에는 최근 수신 된 텍스트가 표시되어야합니다. 하지만 다른 활동은 앱을 처음 시작한 후에받은 첫 번째 텍스트를 항상 표시합니다. 새 문자 메시지가 표시되지 않습니다.방송 수신기에서 활동으로 보류중인 의도 - 이전 데이터 수신

내가 시도한 것 : 계류중인 의도 (고유 한 무작위 int 및 모든 4 개의 업데이트 옵션) 매개 변수로 재생 - 지금까지 도움이되었습니다.

PendingIntent contentIntent = PendingIntent.getActivity(context, **rand.nextInt(50) + 1**, intent, **FLAG_UPDATE_CURRENT**); 

브로드 캐스트 수신기의 관련 코드는 다음과 같습니다. 알림도 트리거되어 매번 새롭고 올바른 텍스트가 표시됩니다.

private void showNotification(Context context) { 
    Intent intent = new Intent(context, DisplayMSG.class); 
    intent.putExtra(EXTRA_MESSAGE, str); 
    Random rand = new Random(); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, rand.nextInt(50) + 1, intent, FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("RPT Notification") 
      .setContentText(str + Integer.toString(n)); 
    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 

} 
} 

그리고 여기가 수신 활동의 부분 :

Intent intent = getIntent(); 
String message = intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE); 
TextView textView = (TextView) findViewById(R.id.tView); 
textView.setText(message); 

당신이 더 많은 코드가 필요하면 알려주세요. 그러나 기본적으로 효과가 있기 때문에 이것이 충분해야한다고 생각합니다. 새로운 텍스트를 전송하지 않고 있습니다.

편집 : 여기 CommonsWare하는 덕분에이 솔루션은, 그냥 수신 활동에이 방법을 추가됩니다

@Override 
protected void onNewIntent(Intent intent) 
{ 
    super.onNewIntent(intent); 
    TextView textView = (TextView) findViewById(R.id.tView); 
    textView.setText(intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE)); 
} 

답변

2

을 그리고 여기에 수신 활동

경우]에서 부분 활동이 이미 실행 중입니다. 즉, 잘못된 것입니다. Intent. onNewIntent()을 덮어 쓰고 전달 된 Intent을 사용하십시오. getIntent()은 항상 액티비티를 생성하는 데 사용 된 Intent을 반환하며, 기존 인스턴스를 포 그라운드로 다시 가져온 후속 액티비티는 반환하지 않습니다.

+0

감사합니다. 5 분 안에 문제가 해결되었습니다. :) – user3503212