Android 中提高后台服务优先级

来源:互联网 发布:aix 安装软件 编辑:程序博客网 时间:2024/05/26 22:56

这篇文章的内容是网上其它大神提供的,之前做项目时记录了下来,但是忘记了是哪位的文章了,所以无法给出转载的地址,望原谅!

by Zhou 2015-5-23

=====================================================================================================

下面是提高Android 服务的几种策略:

  1> <service
            android:name="com.android.vesmart.videorecording.service.RecorderService"
            android:permission="true"
            android:process="com.android.vesmart.videoserver" >
     该方法可以实现service不被系统回收
  2>
public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }  
          服务被杀死后将自动重启
  3>在onDestory()中重新启动服务。
   public void onDestroy() {
     Intent intent = new Intent(this, RecorderService.class);
            intent.putExtra(RecorderService.SERVICE_FIRST_START_KEY,
                    "START_FORM_SELF");
      startService(intent);
   }
  4>
 一般的,如果要启动一个不会被杀死的服务都要通知用户,只要通知了用户以后服务就不会被杀死,但是一旦清除这些通知,服务一样会被杀死,我们这里采取投机取巧的方法,让服务不会被第三方任务管理器杀死。
  public void onCreate() {
    Notification notification = new Notification();   //此处为创建前台服务,但是通知栏消息为空,这样我们就可                              以在不通知用户的情况下启动前台服务了
    startForeground(1, notification);
  }


1 0
原创粉丝点击