无法将字符串解析为通知 Activity
Cannot parse string to a Notification Activity
我有一个 ExternalReceiver class,它接收来自服务的通知:
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);
}
到目前为止它工作正常,因为我的 phone 显示了带有消息的通知。但是当我点击通知时,它进入 NotificationAcivity class:
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);
}
}
}
}
intent 中的 "extras" 值为空,因此 "newMessage" 字符串实际上并未存储。为什么在这种情况下不存储值?。
你好像把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);
我有一个 ExternalReceiver class,它接收来自服务的通知:
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);
}
到目前为止它工作正常,因为我的 phone 显示了带有消息的通知。但是当我点击通知时,它进入 NotificationAcivity class:
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);
}
}
}
}
intent 中的 "extras" 值为空,因此 "newMessage" 字符串实际上并未存储。为什么在这种情况下不存储值?。
你好像把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);