如何在android中为通知项编写onclick llistener?

Rag*_*ddy 2 android

嗨,我是Android的新手.我正在实现具有通知功能的代码.在我的应用程序中,我有两个活动,分别是ActivityA和ActivityB.我想从通知启动ActivityB,我需要向ActivityB发送一些标志或一些值.如何使用click上的通知将int value或flag等数据发送到该被调用的活动.问题是当我从启动器图标启动活动时,它首先调用ActivityA,然后从ActivityA我将一些值传递给ActivityB.但是当我从通知中启动ActivityB时,我不会向该活动发送任何值,因此它会关闭力量.

要从通知中调用活动,我正在使用此代码

mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me";               
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setComponent(new ComponentName("mypackage","mypackage.ActivityB"));

PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Run Code Online (Sandbox Code Playgroud)

请告诉我如何将通知值发送到被叫活动.

Rom*_*ack 5

您必须在notifyIntent中设置ActivityB

Intent notifyIntent = new Intent(this, ActivityB.class); // 'this' - Context object
Run Code Online (Sandbox Code Playgroud)

对于发送值,请使用附加功能,例如:

intent.putExtra("yourTag", yourVariable);
Run Code Online (Sandbox Code Playgroud)