Android应用被killed时,Service不被一起kill的方法
2015-07-12 16:50
471 查看
其实很简单,只需要将服务设置成前台服务即可
stopForeground可以取消服务的前台状态。和startForeground搭配使用,可以控制服务是否跟谁app一起被killed。
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "service on start"); Log.d(MyService.TAG, "MyService main thread:" + Thread.currentThread().hashCode()); Notification notification=new Notification(); notification.tickerText="HAHA"; this.startForeground(startId, notification); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); }
stopForeground可以取消服务的前台状态。和startForeground搭配使用,可以控制服务是否跟谁app一起被killed。
相关文章推荐
- Android Studio SDK Download interrupted: Read timed out解决
- Mac下Android studio 之NDK配置教程(二)
- android 中“分享一下”的实现方法
- Android去掉标题栏和全屏
- Android开发:获取屏幕宽和高的两种方法(px、dip、分辨率之间的关系及px与dip互相转化)
- Android 屏幕(View)坐标系统
- android坐标
- Android基础入门
- 优雅的处理Android数据库升级的问题
- Android fragment
- AndroidStudio打包及引用aar - Android
- Android页面事件挂接模拟
- Android数据存储之IO
- android ActionBar(2)
- Android SharedPreferences 详解
- InstaMaterial-优秀的Android开源项目
- Android开发(三十二)——延时
- Android开发(三十一)——重复引用包错误Conversion to Dalvik format failed
- Android开发(二十九)——layout_weight的含义
- Android开发(二十八)——基础功能函数