android – 点击后删除通知

我刚刚开始处理通知,现在我尝试删除通知,并在通知中心点击通知中心后启动应用程序。

我试图使用下面的代码:

import android.app.NotificationManager;

public class ExpandNotification {
     private int NOTIFICATION = 546;
     private NotificationManager mNM;

     public void onCreate() {
        mNM.cancel(NOTIFICATION);
        setContentView(R.layout.activity_on);
        //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
    }

我认为这个代码执行其他类,当点击?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);

然而,通知没有消失,应用程序也没有启动。
但我可以向左或向右滑动它删除它,但这不是我想要的。

使用标志Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

并启动应用程序:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);

// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
http://stackoverflow.com/questions/12972868/removing-notification-after-click

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:android – 点击后删除通知