点击Notification自动消失的方法。

点击Notification自动消失的方法:添加一句.setAutoCancel(true)即可。

如下面的代码所示:

 public class TestAty extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    

        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        super.onCreate(savedInstanceState);
        setTitle("功能测试界面");
        setContentView(R.layout.aty_test);
        

        Button btnNotify = (Button) findViewById(R.id.btnNotification);
        btnNotify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Context context = getApplicationContext();
                CharSequence contentTitle = "通知栏标题";
                CharSequence contentText = "通知栏详细内容";
                Intent intent = new Intent(TestAty.this, MainAty.class);
                PendingIntent contentIntent = PendingIntent.getActivity(TestAty.this, 0,
                        intent, 0);
                        

                Notification notification = new Notification.Builder(context)
                        .setContentTitle(contentTitle)
                        .setContentText(contentText)
                        .setSmallIcon(R.drawable.test_icon_2)
                        .setWhen(System.currentTimeMillis())
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setContentIntent(contentIntent)
                        .setAutoCancel(true)
                        .build();
                        


                //用mNotificationManager的notify方法通知用户生成标题栏消息通知
                notificationManager.notify(1, notification);
            }
        });
    }
}

你可能感兴趣的:(notification,自动消失)