安卓:intent.putExtra在服务类获得通知(Android: intent.putExtr

2019-09-23 20:51发布

这是服务类我showNotification方法:

private void showNotification() {

Notification notification = new Notification(R.drawable.icon,
"New Notification", System.currentTimeMillis());

Intent i = new Intent(this, myActivity.class);
i.putExtra("notification", "MyNotif");
i.putExtra("notifiedby", "NotedBy");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
notification.setLatestEventInfo(this, "NotedBy", "MyNotif", contentIntent);
nm.notify(111, notification);
}

所以点击状态栏中通知后,我会在myActivity结束。

问题是,这些行总是给人在myActivity

this.getIntent().hasExtra("notification")
this.getIntent().hasExtra("notifiedby")

不putExtra()用的PendingIntent工作吗?

Answer 1:

试试这个代码,而不是:

String notification = getIntent().getStringExtra("notification");
String notifiedby = getIntent().getStringExtra("notifiedby");


文章来源: Android: intent.putExtra in a Service Class to get notifications