我在使用Pending Intent发送额外内容时遇到问题。我在StateCh
服务中添加了额外的字符串并发送到我的MainActivity
。 MainActivity
按预期开始,但我放在那里的额外字符串总是丢失。
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);
}
// ...
}
答案 0 :(得分:1)
使用TextUtils
if (!TextUtils.isEmpty(recMessage)) {
// here your require condition
}
答案 1 :(得分:0)
我找到了正确的答案,我的问题再问一次,更确切地说是另一个topis,在这里:Pending intent extras are received only when activity is paused。也许它会帮助别人。