《第一行代码》第8章 --通知的使用修正
2016-06-05 21:24
176 查看
《第一行代码》第8章的中涉及的通知的基本用法处,由于Notificition已被弃用,所以代码出错。需要更新写法。下面列出的是可用的修正写法。同时,第九章P365页处用到的通知,也需要重新写。
![](https://img-blog.csdn.net/20160605211658011)
更新为
![](https://img-blog.csdn.net/20160605211812882)
![](https://img-blog.csdn.net/20160605212318888)
更新为:
![](https://img-blog.csdn.net/20160605212333342)
1、P300 页源码及修正
@Override private void onClick(View v){ switch (v.getId()){ case R.id.send_notice: NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.s3,"this is ticker text", System.currentTimeMillis()); notification.setLatestEventInfo(this,"this is title","this is text",null); manager.notify(1,notification); break; default: break; } }
更新为
@Override public void onClick(View v) { switch (v.getId()) { case R.id.send_notice: NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.s3); builder.setTicker("just watch me"); builder.setWhen(System.currentTimeMillis()); builder.setContentTitle("look at that"); builder.setContentText("you can do it!"); Intent intent = new Intent(this,NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pi); //Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg")); //builder.setSound(soundUri); //long[] vibrates = {0,1000,1000,1000}; //builder.setVibrate(vibrates); //builder.setLights(Color.GREEN,1000,1000); builder.setDefaults(Notification.DEFAULT_ALL); manager.notify(1, builder.build()); break; default: break;
2、P365 页源码及修正
Notification notification = new Notification(R.drawable.a10,"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); Log.d("MyService","onCreate executed");
更新为:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setTicker("just watch me"); builder.setWhen(System.currentTimeMillis()); builder.setContentTitle("look at that"); builder.setContentText("you can do it!"); startForeground(1,builder.build());
相关文章推荐
- Windows环境下实现Numpy、Scipy、Matlotlib、Scikit-learn的安装
- [GitHub]第四讲:合并分支
- XTEA加密的一种JAVA实现,64轮,CBC模式,0填充
- C语言学习篇-10指针与字符串、函数指针
- c++实验7-特殊三位数
- C++第五次作业
- [GitHub]第三讲:简单分支操作
- Ubuntu下使用openssl编程
- C++实验7--项目2:最大公约和和最小公倍数
- c++第七次实验
- C++第五次实验
- SpringMVC学习(9):实现注解式权限验证
- 从Python中readline()函数读取的一行内容中去掉换行符\n
- java基础之抽象类和接口
- C++中的单例模式
- c++作业7
- 【java】异常处理
- Visual C++ 开发实战1200例 第一章开发环境
- 【java并发】线程同步工具CyclicBarrier的使用
- 《梦断代码》读书笔记(三)