API 23上的notification实现(读书笔记)

来源:互联网 发布:ubuntu安装ssh server 编辑:程序博客网 时间:2024/05/16 04:51

1.坑

在第一行代码提到的notification的实现提到

直接用Notification的构造方法实例化一个notification对象>
Notification notification = new Notification(icon,tickerText,when);
然后,坑来了…>
调用Notification的setLastsetEvenInfo()方法设置一个布局什么的

再然后就在IDE里敲入setLastsetEvenInfo()

根本就没有这个方法!!(/‵Д′)/~ ╧╧,(我用的是Android studio,API level 23,真机调试的系统就是android 6.0~)

2.折腾

网上查资料后发现这个方法早在API 11后就抛弃了 (╬☉д⊙)
Android Studio提示deprecated(大概就是被抛弃了的意思

After API level 11 应该用Notification.Builder来创建Notification

Intent intent = new Intent(MainActivity.this, PendingIntentActicity.class);PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification = new Notification.Builder(MainActivity.this)                        .setContentTitle("This is title")                        .setContentText("This is content text")                        .setWhen(System.currentTimeMillis())                        .setTicker("This is ticker")                        .setSmallIcon(R.mipmap.ic_launcher)                        .setPriority(Notification.PRIORITY_MAX)                        .setContentIntent(pendingIntent)                        .setAutoCancel(true)                        .build();

build()方法返回一个Notification对象

finally

manager.notify(1, notification);
0 0