Android如何使自己的应用不容易被清理掉和开机自动启动

(1)Application加上Persistent属性

看Android文档知道,当进程长期不活动,或系统需要资源时,会自动清理门户,杀死一些Service,和不可见的Activity等所在的进程。但是如果某个进程不想被杀死(如数据缓存进程,或状态监控进程,或远程服务进程),可以这么做:

<application  
    android:allowBackup="true"  
    android:icon="@drawable/ic_launcher"  
    android:label="@string/app_name"  
    android:persistent="true" //设置Persistent
    android:theme="@style/AppTheme" >  
</application>

据说这个属性不能乱设置,不过设置后,的确发现优先级提高不少,或许是相当于系统级的进程

(2)提升service优先级
在AndroidManifest.xml文件中对于intent-filter可以通过android:priority = “1000”这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低,同时适用于广播。

<service  
    android:name="com.hx.doubleprocess.MyService"  
    android:enabled="true" >  
    <intent-filter android:priority="1000" >  
        <action android:name="com.hx.myservice" />  
    </intent-filter>  
</service>

(3)添加开机自启权限

    <!-- 开机广播  设置软件开机自动启动-->
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <!--网络状态改变广播-->
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

猜你喜欢

转载自blog.csdn.net/zxt94/article/details/74305121