小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

三種方法讓你的Service不被“一鍵加速”和系統(tǒng)殺掉

 cathy001 2016-09-12

基本上大家都知道提高service優(yōu)先級(jí)可以在很大程度上讓你的service免于因?yàn)閮?nèi)存不足而被kill,當(dāng)然系統(tǒng)只是在此時(shí)先把優(yōu)先級(jí)低的kill掉,如果內(nèi)存還是不夠,也會(huì)把你的service干掉的.

1.android:persistent="true"

常駐內(nèi)存屬性對(duì)第三方app無(wú)效,下面是官方說(shuō)明

android:persistent

Whether or not the application should remain running at all times — "true" if it should, and "false" if not. The default value is "false". Applications should not normally set this flag; persistence mode is intended only for certain system applications.

2.startForeground將其放置前臺(tái)

Notification notification = new Notification();notification.flags = Notification.FLAG_ONGOING_EVENT;notification.flags |= Notification.FLAG_NO_CLEAR;notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;startForeground(1, notification);

3.可以監(jiān)聽(tīng)I(yíng)ntent.ACTION_TIME_TIC系統(tǒng)時(shí)鐘廣播,系統(tǒng)每隔一段時(shí)間發(fā)送這個(gè)廣播,當(dāng)service被殺死的時(shí)候,隔一段時(shí)間通過(guò)廣播啟動(dòng)

靜態(tài)注冊(cè)android.intent.action.TIME_TICK監(jiān)聽(tīng)

判斷service是否啟動(dòng)

public boolean isServiceRunning(String serviceName)	{		ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 	for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) 	{ 		if(serviceName.equals(service.service.getClassName()))		{ 			return true; 		} 	}	return false;	}

接受到廣播后判斷是否啟動(dòng)該service 若沒(méi)有啟動(dòng)就啟動(dòng)它

if(intent.getAction().equals(Intent.ACTION_TIME_TICK))	{				if (!isServiceRunning(name)) 	{ 		Intent mIntent = new Intent(context, MyService.class); 		context.startService(mIntent); 	}}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多