如何从通知栏中清除或关闭通知在android中单击关闭按钮

如何从通知栏中清除或关闭通知在android中单击关闭按钮,android,notifications,Android,Notifications,我使用下面的代码如何实现我的座右铭,我想关闭或清除通知,从通知栏点击关闭按钮 Intent moreNewsIntent = new Intent(this, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(AppService.this, 0, moreNewsIntent, 0); // expande

我使用下面的代码如何实现我的座右铭,我想关闭或清除通知,从通知栏点击关闭按钮

Intent moreNewsIntent = new Intent(this, MainActivity.class);
                        PendingIntent pIntent = PendingIntent.getActivity(AppService.this, 0, moreNewsIntent, 0);

                        // expanded view of the notification.
                       /* Intent close = new Intent(this, AppService.class);
                        close.setAction(CommonConstants.ACTION_DISMISS);
                        PendingIntent piDismiss = PendingIntent.getService(this, 0, close, 0);*/


                        int notificationId = new Random().nextInt(); // just use a counter in some util class...
                        PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notificationId, AppService.this);


                        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentTitle(getString(R.string.app_name))
                                .setTicker("News Notification")
                                .setContentText(getString(R.string.lbl_notification_title_news))
                                .setDefaults(Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE)
                                .setLights(0xff00ff00, 300, 100)                            
                                .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                                .setContentIntent(pIntent)


                                .addAction (R.drawable.news_negative,getString(R.string.btn_more_nes), pIntent)
                                .addAction (R.drawable.close,getString(R.string.btn_close), dismissIntent)

                                .setAutoCancel(true);
NotificationActivity.java

public class NotificationActivity extends Activity {
    public static final String NOTIFICATION_ID = "001";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
        finish(); // since finish() is called in onCreate(), onDestroy() will be
                    // called immediately
    }

    public static PendingIntent getDismissIntent(int notificationId,
            Context context) {
        Intent intent = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return dismissIntent;
    }

}
AndroidManifest.xml

<activity android:name=".NotificationActivity" android:taskAffinity="" android:excludeFromRecents="true">

如果您使用与初始化通知相同的id呼叫Thanx advance help,我们将不胜感激


如果您询问如何获取PendingEvent来执行cancel()调用,那么看起来您的方法是正确的。更好的方法可能是使用,在清单中注册接收者,然后从清单中进行调用。

您想在已定义的“刷卡关闭”功能的基础上添加关闭通知的功能吗?通常,只为前台服务添加关闭按钮。这属于评论部分!为什么?这是对这个问题的恰当回答。