2013-07-19 35 views
0

我想添加一个点击动作(退出(完成))到通知。 我正在制作一个简单的应用程序与几个类,我希望他们都完成了,当我点击通知。 这里是我的通知码:如何在通知上添加点按操作?

mMN = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification n = new Notification(); 
     n.icon = R.drawable.ic_launcher; 
     n.tickerText = "Tap this notification to exit"; 
     n.when = System.currentTimeMillis(); 
     Intent nid = new Intent(MainActivity.this, stopservice.class); 
     PendingIntent ci = PendingIntent.getActivity(this, 0, nid,PendingIntent.FLAG_UPDATE_CURRENT); 
     CharSequence ct = "TAP"; 
     CharSequence tt = "Tap here to exit"; 
     n.setLatestEventInfo(this,ct,tt,ci); 
     mMN.notify(NOTIFICATION_ID, n); 

我在Intent nid作出参考stopservice class(如果是我的一站式服务代码),但我不能肯定它是否是一个正确的参考。

希望我的问题很清楚。

+0

你能否澄清一点关于你想达到什么目的?您是否试图在通知上实现类似“onClickListener”的功能?如果是这样,则不一定有直接的做法。您将需要创建一个完成您的行动的“意图”。 – Brian

回答

1

您应该使用的通知建设者:

mMN = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);   

Intent nid = new Intent(MainActivity.this, stopservice.class); 
// If you were starting a service, you wouldn't using getActivity() here 
PendingIntent ci = PendingIntent.getActivity(MainActivity.this, NOTIFICATION_ID, nid, PendingIntent.FLAG_UPDATE_CURRENT); 

Notification.Builder builder = new Notification.Builder(MainActivity.this); 
builder.setContentTitle("TAP") 
     .setContentText("Tap here to exit") 
     .setTicker("Tap this notification to exit") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentIntent(ci) 
     .setAutoCancel(true); // auto cancel means the notification will remove itself when pressed 

mMN.notify(NOTIFICATION_ID, builder.getNotification()); 

您的代码将推出的活动“stopservice”(这在技术上应该是“StopService”与命名约定),你确定那个类是活动?

此外,确保您的活动在您的应用程序的清单注册:

<activity android:name="[package-name].stopservice" android:label="@string/app_name"/>