Service防止被Kill的方法整理

在开发过程中,常常需要开启一些后台服务,来处理一些数据操作,即使用户退出程序后,后台也能够正常的处理数据,此时就需要使用到Service,关于Service的使用,在这里就不做详细介绍了,今天讨论的是,即使我们使用Service,也并不一定能保证程序的正常运行,为什么呢?因为市面上存在很多清理进程的软件,比较常见的就是猎豹、360等,而且绝大多数的用户也都会安装这类软件以清理手机内存。当用户使用此类软件清理进程后,绝大多数的Service都被kill掉,如果你的Service非常重要,此时被kill就会导致程序的不正常运行,那么如何保持Service的常驻内存而不被kill掉呢?下面我整理了如下几个方法来防止Service被kill
       

        方法一:提高Service的优先级
                     对于Service被系统回收,一般做法是通过提高优先级可以解决,在AndroidManifest.xml文件中对于intent-filter可以通过android:priority = "1000"这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低,同时实用于广播,推荐大家如果你的应用很重要,可以考虑通过系统常用intent action来触发。 具体代码如下

 
  <service 

            android:name="com.jarvis.widget.OpenServiceForWidget"

            android:enabled="true"

            android:priority = "1000"

            >
            <intent-filter >

                <action android:name="OpenServiceForWidget"/>

            </intent-filter>
            
  </service>
 

        方法二:保持Service前台运行
                       
Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用。当系统觉得当前的资源非常有限的时候,为了保 证一些优先级高的程序能运行,就会杀掉一些他认为不重要的程序或者服务来释放内存。这样就能保证真正对用户有用的程序仍然再运行。如果你的 Service 碰上了这种情况,多半会先被杀掉。但如果你增加 Service 的优先级就能让他多留一会,我们可以用 setForeground(true) 来设置 Service 的优先级。 
 
在一个开启的Service中的OnCreat方法中设置,前台运行(此方法不会在通知栏显示前台运行的Service信息): 
Notification notification = new Notification();

startForeground(1notification);
 
当然,如果你需要让用户知道你的这个Service正在运行,可以在通知栏中显示一个标签,告诉用户此Service正在运行:
 
 Notification notification = new Notification(R.drawable.ic_launcher""System.currentTimeMillis());

        notification.flags|= Notification.FLAG_NO_CLEAR;  

        notification.flags=Notification.FLAG_ONGOING_EVENT;

        Intent notificationIntent = new Intent(thisMainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this0notificationIntent0);

        notification.setLatestEventInfo(this"service"""pendingIntent);     

        startForeground(1 , notification);
 
如果此时你又想使Service在后台运行,我们可以调用这个方法来设置:

    //(kill

    // notification 便)

        NotificationManager notificationManager=(NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.cancel(1);

        //使Service

        stopForeground(false);
 

 方法三:
在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建
 
    @Override

    public int onStartCommand(Intent intentint flagsint startId) {

        return START_STICKY;
    }
 
 在Service的onDestroy()中重启Service.
public void onDestroy() {   

        Intent localIntent = new Intent();

        localIntent.setClass(thisService.class);  //Service

        this.startService(localIntent);
    }
 

方法四:不可滥用的方法,此方法的使用需慎重
 
   //AndroidManifest.xmlandroid:persistent="true"   
   <application

        android:persistent="true"        

        >
            
        </application>
 
 切记,这个不可滥用,系统中用这个的service,app一多,整个系统就完蛋了。
目前系统中有phone等非常有限的,必须一直活着的应用在使用。



参考:
http://blog.csdn.net/qinjuning/article/details/6915482
            http://blog.csdn.net/yuzhiboyi/article/details/8484771   PendingIntent的理解
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值