Android:用于获取通知的服务类中的intent.putExtra

Arc*_*pgc 5 service android android-intent

这是我在Service类中的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);
}
Run Code Online (Sandbox Code Playgroud)

所以点击后通知状态栏,我将结束myActivity.

问题是这些行总是在myActivity中给出错误.

this.getIntent().hasExtra("notification")
this.getIntent().hasExtra("notifiedby")
Run Code Online (Sandbox Code Playgroud)

putExtra()不能与PendingIntent一起工作吗?

Anu*_*kur 6

请尝试使用此代码:

String notification = getIntent().getStringExtra("notification");
String notifiedby = getIntent().getStringExtra("notifiedby");
Run Code Online (Sandbox Code Playgroud)