Android初级工程师的service研究之路
来源:互联网 发布:混元一气太乙金仙 知乎 编辑:程序博客网 时间:2024/05/19 05:04
Android初级工程师的service研究之路
最近感觉挺闲的,在公司工作一年多了,大多时间都在写网页,微信小程序,发现Android的东西写得越来越少了,好多东西都快忘了,现在来复习一下,就发现,Android四大组件的service我快不知道怎么写了。
今天特地去看了下以前收藏的郭神写得service文章,写得特别好,但是发现由于时间的原因,还是有一些坑在里面了,特别是方法的废弃。
主要来说明下方法废弃之后,用什么方法代替。
首先来看看以前的一种写法
public class MyService extends Service { @Override public void onCreate() { super.onCreate(); Notification notification = new Notification(R.drawable.ic_launcher, "前台service通知测试", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, ServiceActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "前台service通知标题", "前台service通知内容", pendingIntent); startForeground(0, notification); }}
这个写好之后运行时,我发现service没有起到作用,通知并没有出现。
后来我查阅资料后发现,最好写一个NotificationManager来作为Notification的管理器。改变后是这样的。
public class MyService extends Service { @Override public void onCreate() { super.onCreate(); NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "前台service通知测试--旧方法", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, ServiceActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "前台service通知标题--旧方法", "前台service通知内容--旧方法", pendingIntent); manager.notify(0, notification); }}
这样更改之后,运行发现,果然能实现了前台service,通知出现了
实现了通知,但是又发现一个问题,就是这个写法里面的
Notification notification = new Notification(R.drawable.ic_launcher, "前台service通知测试--旧方法", System.currentTimeMillis());
Notification方法被打横线了,这就说明,Notification被废弃了,而且不止这一个方法,还有
notification.setLatestEventInfo(this, "前台service通知标题--旧方法", "前台service通知内容--旧方法", pendingIntent);
中的setLatestEventInfo方法也被废弃了。
那么就只有找其他方法来进行操作了。经过努力后我将代码改成了这样
public class MyService extends Service { @Override public void onCreate() { super.onCreate(); //Notification的管理器 NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); //新建Notification.Builder对象 Notification.Builder builder = new Notification.Builder(this); //设置点击通知的跳转页面 Intent notificationIntent = new Intent(this, ServiceActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //设置通知的图标 builder.setSmallIcon(R.drawable.ic_launcher); //添加弹出通知时候的信息 builder.setTicker("前台service通知测试--新方法"); //设置通知的标题 builder.setContentTitle("前台service通知标题--新方法"); //设置通知的内容 builder.setContentText("前台service通知内容--新方法"); //执行intent builder.setContentIntent(pendingIntent); //将Notification.Builder对象转为普通的Notification对象 Notification notification = builder.build(); //设置点击通知后通知会消失(Notification有各种参数,大家可自己尝试效果) notification.flags |= Notification.FLAG_AUTO_CANCEL; //运行Notification manager.notify(0, notification); }}
如此写好之后,就会发现没有被废弃的方法,而且service也是可以执行的,看看效果图
本人第一次写博客,有不足之处,欢迎大家批评指出,谢谢······
阅读全文
0 0
- Android初级工程师的service研究之路
- iOS初级开发工程师进阶之路
- android的service的研究
- Android工程师的实践之路:工程师的学习路线
- android Application Component研究之Service
- android Application Component研究之Service
- android Application Component研究之Service
- Android Service之onStartCommand方法研究
- android Application Component研究之Service
- 初级前端工程师第一步之HTML5改变的认知
- SQLite的初级研究
- android开发 关于Service的研究
- Android Service 研究
- 系统工程师之系统升级研究
- Android初级工程师面试笔试题
- Android面试题【初级工程师版】
- Android面试题【初级工程师版】
- 【android】:android工程师的学习实践之路
- Godeyes 使用说明
- CentOS7引导Windows7
- 个人喜欢的关于模式识别、机器学习、推荐系统、图像特征、深度学习、数值计算、目标跟踪等方面个人主页及博客
- 微信公众号开发之获取地理位置接口
- python中获取异常描述与else用法
- Android初级工程师的service研究之路
- GMS认证之CtsVerifier测试-Screen Pinning Test
- SVN不同版本库迁移
- C++多态与继承
- 赌你半天都想不出的Bug
- 从无到有,关于RxJava的详解
- Python之文件读写
- Docker 设置proxy
- 如何破解TeamView