android提高进程优先级的方法

本文介绍了三种提高Android进程优先级的方法:通过startForeground和stopForeground调整进程优先级;在AndroidManifest.xml中设置persistent属性;使用Broadcast Intent来临时提高进程优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法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"   <!--新增加代码,保证acore进程不被ActivityManager杀死--> 
     >

 

方法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 。 

转载于:https://www.cnblogs.com/xxdc/p/3663969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值