android提高进程优先级的若干方法

来源:互联网 发布:淘宝的拍照权限设置 编辑:程序博客网 时间:2024/06/06 03:20

转载自:http://www.2cto.com/kf/201402/279086.html,在此表示感谢

方法1: 提高进程优先级 startForeground(1, new Notification()); 降低进程优先级 stopForeground(true); 

onStart() 方法中进行提高优先级操作,然后在onStop()方法中进行降低优先级操作
这个方法可以将对应AP的ADJ临时提高到2。

方法2
找到这个进程对应的AndroidMannifest.xml文件,在其中添加属性『android:persistent="true"』, 这样可以将该进程设置为常驻内存进程,就可以降低被Kill的概率。以Acore进程为例, 在 /package/providers/ContactsProvider/AndroidMannifest.xml 文件中增加一行『android:persistent="true"』
具体修改示例如下:
<application android:process="android.process.acore"
android:label="@string/app_label"
android:icon="@drawable/app_icon"
android:allowBackup="false"
android:persistent="true" 
方法3:
在需要提高优先级的地方调用: 
final Intent it = new Intent(); 
it.setAction("android.intent.action.BOOST_DOWNLOADING"); 
it.putExtra("package_name", "com.android.contacts"); 
it.putExtra("enabled", true); 
context.sendBroadcast(it); 
在需要恢复到正常优先级时调用: 
final Intent it = new Intent(); 
it.setAction("android.intent.action.BOOST_DOWNLOADING"); 
it.putExtra("package_name", "com.android.contacts"); 
it.putExtra("enabled", false); 
context.sendBroadcast(it); 
onStart() 方法中进行提高优先级操作,然后在onStop()方法中进行降低优先级操作
这个方法可以将对应AP的ADJ临时提高到7。
0 0