第一行代码学习笔记---过时的通知写法

来源:互联网 发布:linux安装lnmp 编辑:程序博客网 时间:2024/06/04 16:37

8.29更新—>关于通知的文章
http://blog.csdn.net/vipzjyno1/article/details/25248021
今天读到第一行那个代码365页的服务通知写法。发现里面很多方法已经过时甚至没有了。

书本代码

:

Notification notification = new Notification(R.drawable.ic_launcher, "Notification comes", System. currentTimeMillis());          Intent notificationIntent = new Intent(this, MainActivity.class);          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);          notification.setLatestEventInfo(this, "This is title", "This is          content", pendingIntent);          startForeground(1, notification);  

其中Notification实例化方式过时,并且没有setLatestEventInfo方法了。

经查资料,原来要引入一个Notification.Builder builder 来实例化Notification

新代码

:
具体如下:

Intent notificationIntent=new Intent(this,MainActivity.class);        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);        Notification.Builder builder = new Notification.Builder(this);        Notification notification=builder.setContentIntent(pendingIntent)                .setSmallIcon(R.mipmap.ic_launcher)                .setWhen(System.currentTimeMillis()).setAutoCancel(false)                .setContentTitle("statusContentTitle").setContentText("statusContentContext").build();startForeground(1,notification);

使用Builder set各种设置,最后.buid()。

测试:
这里写图片描述

补充

builder.getNotification()也是过时的方法, 最好不要再用了。

今天翻到了300页的通知写法,果然也是过时的。

书本代码:

    Notification notification = new Notification(R.drawable.strawberry,"this is ticker text",System.currentTimeMillis());      notification.setLatestEvenInfo(this,"This is content title","This is context text",null);    manager.notify(1,notofication);

修改代码:

//获取系统通知的管理类,负责发通知、清除通知等操作。 NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);mBuilder.setContentTitle("测试标题")//设置通知栏标题                        .setContentText("测试内容") //设置通知栏显示内容</span>               // .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图                    //  .setNumber(number) //设置通知集合的数量                    .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的                    .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间                    .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级                    //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消                    .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)                    .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合                    //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission                    .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICONNotification notification = mBuilder.build();notification.flags = Notification.FLAG_AUTO_CANCEL;mNotificationManager.notify(1, mBuilder.build());

这里写图片描述

0 0
原创粉丝点击