收到时,缺少有意图的额外内容

时间:2013-05-14 04:59:03

标签: android android-pendingintent

我在使用Pending Intent发送额外内容时遇到问题。我在StateCh服务中添加了额外的字符串并发送到我的MainActivityMainActivity按预期开始,但我放在那里的额外字符串总是丢失。

MainActivity.java:

public class MainActivity extends Activity {

 public void onResume() {
    super.onResume();

 String recMessage = this.getIntent().getStringExtra("message");

 if(recMessage.equals("")) {
    Log.v("recMessage", "none");
  } else {
   Log.v("recMessage", "something");
  }
    // ..
 }
}

StateCh.java:

public class StateCh extends Service {

//...

   private void notificationU(String title, String text)  {

    //The intent to launch when the user clicks the expanded notification
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("message", "ssss");
    intent.setAction("actionstring" + System.currentTimeMillis());

    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

     Notification noti2 = new NotificationCompat.Builder(this)
     .setContentTitle(title)
     .setContentText(text)
     .setSmallIcon(R.drawable.warning)
     .setContentIntent(pendIntent)
     .build();

     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     mNotificationManager.notify(123456, noti2);
    }

    // ...      

}

2 个答案:

答案 0 :(得分:1)

使用TextUtils

进行检查
if (!TextUtils.isEmpty(recMessage)) {

// here your require condition

}

答案 1 :(得分:0)

我找到了正确的答案,我的问题再问一次,更确切地说是另一个topis,在这里:Pending intent extras are received only when activity is paused。也许它会帮助别人。