Android--Android6.0版的Notification配置

来源:互联网 发布:汽车内饰件图解知乎 编辑:程序博客网 时间:2024/05/22 00:26

Android6.0以后,Notification的配置有了很大的变化:

初始化一个Notification

private void addNotification() {        Intent intent = new Intent(this, Main2Activity.class); //点击了之后进入的一个Actity        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);        Notification noti = new Notification.Builder(this)                .setTicker("你懂的!")                .setContentTitle("短信通知")                .setContentText("亲爱的,晚上8点老地方见哦~")                .setSmallIcon(R.drawable.icon)//设置图标                     .setDefaults(Notification.DEFAULT_SOUND)//设置声音                .setContentIntent(pendingIntent)//点击之后的页面                .build();        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);        manager.notify(666,noti);    }

取消Notification:
注意取消的ID与,初始化时的ID是一样的。

NotificationManager manager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);                manager.cancel(R.drawable.icon);                manager.cancel(666);                Toast.makeText(getApplication(), "Notification cancled", Toast.LENGTH_SHORT).show();