0

SMS 텍스트를 수신하는이 응용 프로그램을 만들고 싶으면 알림을 표시하고 사용자가 클릭하면 새 활동이 팝업됩니다 SMS 메시지와 함께. 그래서 SMS 텍스트를 수신 할 때 작동하는 BroadcastReceiver 클래스와 같은 기능을 수행하면 모든 기능이 잘 작동합니다. 모든 알림을 멋지게 표시합니다.알림을받을 때 새로운 활동을 얻는 데 문제가 발생했습니다.

public class SMSReceiver extends BroadcastReceiver { 

    static final int notificationId = 1; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     final Bundle bundle = intent.getExtras(); 

     try { 

      if (bundle != null) { 

       final Object[] pdusObj = (Object[]) bundle.get("pdus"); 

       for (int i = 0; i < pdusObj.length; i++) { 

        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
        String phoneNumber = currentMessage.getDisplayOriginatingAddress(); 

        String message = currentMessage.getDisplayMessageBody(); 
        newNotification(context, phoneNumber, message); 

       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    private void newNotification(Context context, String title, String text) { 

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

     Intent notifyIntent = new Intent(context, NotificationActivity.class); 
     notifyIntent.putExtra("SMSText", text); 
     notifyIntent.setAction(text); 
     notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0); 

     Notification notification = new NotificationCompat.Builder(context) 
       .setContentIntent(contentIntent) 
       .setContentText(text) 
       .setContentTitle(title) 
       .setTicker(text) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setWhen(System.currentTimeMillis()) 
       .build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notifyMgr.notify(notificationId, notification); 

    } 

} 

유일한 문제는 알림을 클릭했을 때입니다. 처음 완벽하게 작동하면 NotificationActivity 클래스가 SMS 메시지를 표시합니다. 그러나 그 후에는 정보가 업데이트되지 않습니다. 나는 앱이 이미 NotificationActivity가 열려 있다는 것을 깨닫고 다른 앱을 열지 않는다고 가정합니다. 새 알림을 만들 때 플래그 FLAG_ACTIVITY_NEW_TASK을 올바르게 추가하더라도. 또한 여기 내 NotificationActivity 클래스

public class NotificationActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 

     String newString; 
     if (savedInstanceState == null) { 
      Bundle extras = getIntent().getExtras(); 
      if (extras == null) { 
       newString = null; 
      } else { 
       newString = extras.getString("SMSText"); 
      } 
     } else { 
      newString = (String) savedInstanceState.getSerializable("SMSText"); 
     } 

     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setText(newString); 

    } 
} 

나는 또한 런처 활동, MainActivity을 할 수 있지만 않습니다 모두가 SMSReceiver를 등록하기 때문에 보여줄 필요 느꼈다. 어떤 도움이라도 대단히 감사하겠습니다.

+0

내 업데이트 대답 – mehul

답변

1

당신의 새로운 Intent 원래 의도는 getIntent 방법을 사용하여 유지 될 것 같은 Activity로 전송 될 때마다 호출되지만이 하나가 호출됩니다 onNewIntent 방법을 재정의하는 시도 할 수 있습니다 새로운 Intent이있을 때마다.

@Override 
protected void onNewIntent(Intent intent) 
{ 
    //Do what you want to do with this new Intent here! 
} 
+0

이 거기에 테스트 로그 디버그 메시지를 던졌다'사용하여 작동하지 않는 새로운'NotificationActivity' 클래스 여전히 – Jacob

+0

자, 다시 코드를 통과 시키십시오! 그래서 관련 코드를 붙여 넣을 수 있습니다! 하지만 논리적으로는 onCreate()에서했던 것과 같을 것입니다 – Xenolion

+0

신경 쓰지 마세요. 작동하도록했습니다! 엑스트라를 잡기 위해'intent.getExtras()'를 사용하고 textView 텍스트를 업데이트했습니다. 감사! – Jacob

0

변경이 줄을

PendingIntent contentIntent = PendingIntent.getActivity(context, 
    notificationId , 
    notifyIntent, PendingIntent.FLAG_ONE_SHOT);); 
+0

여전히 PendingIntent.FLAG_ONE_SHOT' – Jacob

+0

를 시작하지 않는보고, 올바르게 실행할 것 ,하지만 여전히 안드로이드에 꽤 새로운 메신저 같아요 ... 내가 그 기능을 몸에 넣어해야합니까? – Jacob

관련 문제