最近在使用android 4.1系统的时候,发现在手机休眠一段时间后(1-2小时),后台运行的服务被强行kill掉,有可能是系统回收内存的一种机制,要想避免这种情况可以通过startForeground让服务前台运行,当stopservice的时候通过stopForeground去掉。
以下是android官方描述:
Running a Service in the Foreground
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.
To request that your service run in the foreground, call startForeground(). This method takes two parameters: an integer that uniquely identifies the notification and the Notification for the status bar. For example:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION, notification);
To remove the service from the foreground, call stopForeground(). This method takes a boolean, indicating whether to remove the status bar notification as well. This method does not stop the service. However, if you stop the service while it's still running in the foreground, then the notification is also removed.
Note: The methods startForeground() and stopForeground() were introduced in Android 2.0 (API Level 5). In order to run your service in the foreground on older versions of the platform, you must use the previoussetForeground() method—see the startForeground() documentation for information about how to provide backward compatibility.
For more information about notifications, see Creating Status Bar Notifications.
要想实现需求,我们只需要在onStartCommand里面调用 startForeground,然后再onDestroy里面调用stopForeground即可!
实际情况就譬如手机里面的音乐播放器一样,不管手机如何休眠,只要开始播放音乐了,就不会kill掉这个服务,一旦停止播放音乐,服务就可能被清掉。
分享到:
相关推荐
Android防止service多次执行startCommand 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/78211385
通过AccessibilityService实现系统按键监听,可实现抢红包插件等.
看Android的文档知道,当进程长期不活动,或系统需要资源时,会自动清理门户,杀死一些Service,和不可见的Activity等所在的进程。但是如果某个进程不想被杀死(如数据缓存进程,或状态监控进程,或远程服务进程),...
南开17春学期《Web Service应用系统设计》在线作业 免费答案.pdf南开17春学期《Web Service应用系统设计》在线作业 免费答案.pdf南开17春学期《Web Service应用系统设计》在线作业 免费答案.pdf南开17春学期《Web ...
南开17春学期《Web Service应用系统设计》在线作业 免费答案.docx南开17春学期《Web Service应用系统设计》在线作业 免费答案.docx南开17春学期《Web Service应用系统设计》在线作业 免费答案.docx南开17春学期《Web...
启明星高校后勤报修系统Servicedesk 微信版可用于高校后勤的水、电、气、门窗等报修。系统采用.NET开发,支持和微信公众号集成。学生关注后,即可进行故障报修。报修后,总务可以派单,维修师傅接单后,维修。 系统...
不被杀死的 安卓service com.lzg.strongService-master
Android Service被关闭后自动重启,解决被异常kill 服务
android系统级的服务包含了Android Service 和Native Service . Android Service 也称作 Java Service ,是用JAVA语言编写的,实现在框架层。 Native Service 也称作 System Service ,是用C++语言编写的,实现...
基于Web Service的系统集成技术在网络教育平台中的应用
华为ManageOne ServiceCenter系统管理指南.pdf
ServiceCD 是一个操作系统的安装引导工具,简化了工程师在本地安装操作系统和驱动 的过程。ServiceCD 通过自动引导操作系统、设备驱动程序及补丁安装等功能将安装过 程中的人机交互降到最低程度,能够有效缩短本地...
Windows系统服务(Service)安装卸载管理删除工具Windows系统服务(Service)安装卸载管理删除工具 ViewService最新版Windows系统服务(Service)安装卸载管理删除工具 ViewService最新版
通过AccessibilityService实现对系统按键的监听功能
第三方apk监听系统按键事件
启明星故障报修系统Servicedesk适用高校、医院等后勤对所属机构的水、电、等报修。 系统采用“用户报修-总务派工-师傅接单-师傅维修-用户评价”流程,使用手机短信通知. 启明星故障报修系统Servicedesk v25.0 更新...
设计好的远程抄表监控终端作为一个网络接点,分配给其相应的网络地址,实现资源服务的分布式系统的集成。客户端在.NET平台下用Visual C#语言开发,通过调用Web Service来进行远程控制、获得所需的数据并按照用户的...
Android系统的Binder机制之一——Service_Manager
用Windows Media Service打造的流媒体直播系统.docx
索爱X10更新安卓2.3系统Update Service