0

У меня есть класс ExternalReceiver, который получает уведомления от службы:Невозможно разобрать строку в Notification активность

public class ExternalReceiver extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
     if(intent!=null){ 
       MessageReceivingService.saveToLog(intent, context);    
     } 
    } 
} 

Вызываемый метод saveToLog хранит уведомляющее сообщение:

protected static void saveToLog(Intent intent, Context context) 
{ 

    String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE); 

    final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent exintent = new Intent (context, NotificationActivity.class);  
    intent.putExtra("NotificationMessage", newMessage); 
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, exintent, PendingIntent.FLAG_UPDATE_CURRENT); 
    final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Besked fra Salon") 
      .setContentText(newMessage) 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true) 
      .getNotification(); 


    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notification.defaults |= Notification.DEFAULT_SOUND; 

    mNotificationManager.notify(R.string.notification_number, notification);    
} 

Он отлично работает так поскольку мой телефон отображает уведомление с сообщением. Но когда я нажав на уведомление, оно входит в класс NotificationAcivity:

public class NotificationActivity extends Activity { 
TextView textViewAc; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notification); 

    textViewAc = (TextView) findViewById(R.id.textViewAc); 
      findViewById(R.id.textViewr1n1); 

    onNewIntent(getIntent());    
} 

@Override 
public void onNewIntent(Intent intent){ 
    Bundle extras = intent.getExtras(); 
    if(extras != null){ 
     if(extras.containsKey("NotificationMessage")) 
     { 
      // extract the extra-data in the Notification 
      String msg = extras.getString("NotificationMessage"); 
      textViewAc.setText(msg);  
     } 
    } 


    } 
} 

«дополнительные» значение от намерения равно нулю, поэтому строка «NewMessage» на самом деле не хранится. Почему значение не сохраняется в этом случае?.

ответ

1

кажется, что вы установили exintent в NotificationActivity и добавлении putextra к intent, поэтому изменению

intent.putExtra("NotificationMessage", newMessage); 
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

к

exintent.putExtra("NotificationMessage", newMessage); 
    exintent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
+0

Такой глупой ошибки, я сделал, спасибо –

+0

@OsmanEsen СЧАСТЛИВЫ помощь, пользоваться. –

Смежные вопросы