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

时间:2014-12-10 03:07:01

标签: android notifications

enter image description here

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

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">

提前帮助Thanx将非常感激

1 个答案:

答案 0 :(得分:-1)

使用与您初始化通知相同的ID呼叫cancel()

如果您正在询问如何让PendingIntent进行取消()调用,看起来您使用PendingIntent.getService()走在正确的轨道上。更好的方法可能是使用PendingIntent.getBroadcast(),在清单中注册接收器,然后从那里进行呼叫。