Android Foreground Service (前台服务)

一、如何保活后台服务

Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务。但是后台服务在系统内存不足的时候,可能会被系统杀死。那么如何让后台服务尽量不被杀死呢?基本的解决思路主要有以下几种:

1. 提高Service的优先级:为防止Service被系统回收,可以尝试通过提高服务的优先级解决,android:priority="1000" ,1000是最高优先级,数字越小,优先级越低。

2.把service写成系统服务,将不会被回收:在Manifest.xml文件中设置persistent属性为true,则可使该服务免受out-of-memory killer的影响。但是这种做法一定要谨慎,系统服务太多将严重影响系统的整体运行效率。 

3.将服务改成前台服务 Foreground service:重写onStartCommand方法,使用StartForeground(int,Notification)方法来启动service。  对于通过startForeground启动的service,onDestory方法中需要通过stopForeground(true)来取消前台运行状态。 

4.利用Android的系统广播:利用Android的系统广播检查Service的运行状态,如果被杀掉,就再起来,系统广播是Intent.ACTION_TIME_TICK,这个广播每分钟发送一次,我们可以每分钟检查一次Service的运行状态,如果已经被结束了,就重新启动Service。 

二、分析为何后台服务会被回收

当后台服务被回收的时候,我们查看Logcat里面的日志的时候,我们可能会看到如下的日志:

06-19 08:01:32.755 W/ActivityManager( 2081): Killing ProcessRecord{43a96570 6437:com.example.helloandroid/u0a187}: background ANR 
06-19 08:01:32.910 I/ActivityManager( 2081): Process com.example.helloandroid (pid 6437) (adj 0) has died. 

这里我们就知道了,ANR导致的,如何避免ANR,主要注意以下几点会导致ANR的发生:

  • 主线程 (“事件处理线程” / “UI线程”) 在5秒内没有响应输入事件 
  •  BroadcastReceiver 没有在10秒内完成返回 
  • 在主线程内进行网络操作 
  • 在主线程内进行一些缓慢的磁盘操作(I/O操作或数据库操作) 

至于如何避免ANR的发生,各位尽量避免上面这几种情况出现,基本上就能避开大部分ANR了。

三、前台服务

1.what

前台服务是那些被认为用户知道(用户所认可的)且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下——这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。

2.why

在一般情况下,Service几乎都是在后台运行,一直默默地做着辛苦的工作。但这种情况下,后台运行的Service系统优先级相对较低,当系统内存不足时,在后台运行的Service就有可能被回收。
那么,如果我们希望Service可以一直保持运行状态且不会在内存不足的情况下被回收时,可以选择将需要保持运行的Service设置为前台服务。

3.how

本文按照做音乐播放器的思路,做一下相关的说明,如何使用前台服务。
 
首先要创建一个服务:
public class MusicPlayerService extends Service {
  
  private static final String TAG = MusicPlayerService.class.getSimpleName();
  
  @Override
  public void onCreate() {
      super.onCreate();
      Log.d(TAG, "onCreate()");
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand()");
  }
  
  @Override
  public IBinder onBind(Intent intent) {
       Log.d(TAG, "onBind()");
       // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
  }
}

然后创建Notification:

在Service的onStartCommand中添加如下代码:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  Log.d(TAG, "onStartCommand()");
  // 在API11之后构建Notification的方式
  Notification.Builder builder = new Notification.Builder
    (this.getApplicationContext()); //获取一个Notification构造器
  Intent nfIntent = new Intent(this, MainActivity.class);
  
  builder.setContentIntent(PendingIntent.
    getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
      R.mipmap.ic_large)) // 设置下拉列表中的图标(大图标)
    .setContentTitle("下拉列表中的Title") // 设置下拉列表里的标题
    .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
    .setContentText("要显示的内容") // 设置上下文内容
    .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
  
  Notification notification = builder.build(); // 获取构建好的Notification
  notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
}

在完成Notification通知消息的构建后,在Service的onStartCommand中可以使用startForeground方法来让Android服务运行在前台。

// 参数一:唯一的通知标识;参数二:通知消息。
startForeground(110, notification);// 开始前台服务

如果需要停止前台服务,可以使用stopForeground来停止正在运行的前台服务。

@Override
public void onDestroy() {
  Log.d(TAG, "onDestroy()");
  stopForeground(true);// 停止前台服务--参数:表示是否移除之前的通知
  super.onDestroy();
}

到此为止,我们基本上就学会了如何使用前台服务了。

四、总结

1. 前台服务与普通服务的区别

  • 前台Service的系统优先级更高、不易被回收;
  • 前台Service会一直有一个正在运行的图标在系统的状态栏显示,下拉状态栏后可以看到更加详细的信息,非常类似于通知的效果。

2.  拓展阅读:

posted @ 2018-03-15 18:29  灰色飘零  阅读(89561)  评论(2编辑  收藏  举报