防止android service 被回收


在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建
  @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                // TODO Auto-generated method stub
                Log.v("TrafficService","startCommand");
                
                flags =  START_STICKY;
                return super.onStartCommand(intent, flags, startId);
        }

如何实现一个不会被杀死的进程
看Android的文档知道,当进程长期不活动,或系统需要资源时,会自动清理门户,杀死一些Service,和不可见的Activity等所在的进程。
但是如果某个进程不想被杀死(如数据缓存进程,或状态监控进程,或远程服务进程),应该怎么做,才能使进程不被杀死。
add android:persistent="true" into the <application> section in your AndroidManifest.xml
切记,这个不可滥用,系统中用这个的service,app一多,整个系统就完蛋了。
目前系统中有phone等非常有限的,必须一直活着的应用在试用。

如何防止Android应用中的Service被系统回收?       
  很多朋友都在问,如何防止Android应用中的Service被系统回收?下面简单解答一下。

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

猜你喜欢

转载自blog.csdn.net/fghydx/article/details/48628099