最近在使用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掉这个服务,一旦停止播放音乐,服务就可能被清掉。

FYI

更多相关文章

  1. tcping测试服务器TCP端口
  2. 【Android话题-2.5系统服务】ServiceManager启动和工作原理是怎
  3. AIDL 消息通信
  4. ionic-android、ios账户绑定手机标识
  5. 详解Android提交数据到服务器的两种方式四种方法
  6. Android(安卓)Service 的一些笔记
  7. Android客户端和服务器端数据交互的第二种方法
  8. Android通过手机GPS获取经纬度方法
  9. Android(安卓)lbs位置服务

随机推荐

  1. android手机游戏之绘图
  2. 解决unity 打包在Android(安卓)8.0上启动
  3. Buttons in button bars should be borde
  4. android 入门学习笔记 正则匹配 电话号码
  5. Android数据库更新并保留原来数据的实现
  6. Android强制竖屏
  7. Android(安卓)Stduio 3.0进行JNI和NDK开
  8. Android开发_多线程详解(九)
  9. android中获取设备的型号和手机厂商
  10. Android(安卓)UI设计 ViewPager 中嵌套Ho