我正在Activity
从我的Service
. 每当发生事件以及每次通过 Intent 传递 Serializable 对象时,我都会这样做。这里的问题是,当第二次调用 Activity 时,它有旧的 Intent 数据而不是新的 Intent 数据。所以我确信这是由于我在Activity
课堂上犯了一些错误,但我无法弄清楚。
public class ReceiveActivity extends Activity {
AlertDialog alertDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Event");
CustomEvent custom= (CustomEvent) getIntent().getSerializableExtra("custom");
alertDialog.setMessage(custom.getName());
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ReceiveActivity.this.finish();
}
});
alertDialog.show();
}
@Override
protected void onPause() {
if(alertDialog!=null) {alertDialog.dismiss();}
super.onPause();
}
@Override
protected void onStop() {
if(alertDialog!=null) {alertDialog.dismiss();}
super.onStop();
}
这是我用来从服务中调用活动的代码(通过 a Notification
)
Notification notification = new Notification(R.drawable.ic_launcher, "msg",
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
Intent incoming =new Intent(this, ReceiveActivity.class);
incoming.putExtra("custom",custom);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,incoming, 0);
notification.setLatestEventInfo(this, "msg","incoming", contentIntent);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("Incoming", count++,notification);
}