【问题标题】:Pending Intent extras are missing when receive接收时缺少待处理的 Intent 额外内容
【发布时间】:2013-05-20 00:33:08
【问题描述】:

我在发送带有待处理意图的附加内容时遇到问题。我在我的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);
    }

    // ...      

}

【问题讨论】:

标签: android android-pendingintent


【解决方案1】:

使用TextUtils检查

if (!TextUtils.isEmpty(recMessage)) {

// here your require condition

}

【讨论】:

  • 这不是问题的原因。如果我用Log.v("message=",recMessage); 替换此签入代码,我会得到message=
【解决方案2】:

我在另一个主题中再次更准确地询问了我的问题,在这里找到了正确的答案:Pending intent extras are received only when activity is paused。也许它会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2018-03-18
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-05-27
    • 1970-01-01
    相关资源
    最近更新 更多