基本概念
進程的優先級
安卓系統定義了了5個進程等級,當內存不不
⾜足的情況下按照5個優先級的不不同來決定到底先殺哪個進程。
如圖所示,優先級從大到小爲
1.前臺進程:
Activity.onResume()
與Service綁定的Activity
被主動設置爲前臺模式的Service.startForeground()
正在執行生命週期的回調的組件(onCreate()/onStart()/onDestroy())
正在執行BroadcastReceiver.onReceive()
2.可見進程
Activity.onPause()
3.服務進程
通過startService啓動的服務
4.後臺進程
Activity.stop()
5.空進程
不包含任何活動的組件.onStop
如何提升進程的優先級
爲了實現提高app的存活率,我們的核心思路就是極可能的提高app進程的優先級.
例如qq掛一個像素點在前臺,讓其成爲前臺進程
實現像QQ一樣掛一個像素點在屏幕上
實現只有一個像素點的頁面
public class EmptyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
WindowManager.LayoutParams params = window.getAttributes();
params.height=100;
params.width=100;
window.setAttributes(params);
}
}
<style name="KeepAlive">
<!
<item name="android:windowBackground">@android:color/transparent</item>
<!
<!
<!
<item name="android:windowNoTitle">true</item>
<!
<item name="android:windowIsFloating">true</item>
<!
<item name="android:backgroundDimEnabled">false</item>
<!
<item name="android:windowContentOverlay">@null</item>
<!
<item name="android:windowIsTranslucent">true</item>
<!
<item name="android:windowAnimationStyle">@null</item>
<!
<item name="android:windowDisablePreview">true</item>
<!
<item name="android:windowNoDisplay">false</item>
</style>
在配置文件中應用
<activity android:name=".EmptyActivity"
android:theme="@style/KeepAlive">
創建一個空頁面的管理器,實現在服務中打開應用
public class ActManager {
private static ActManager mInstance;
private WeakReference<Activity> mReference;
private ActManager() {
}
public static ActManager getInstance(){
if (mInstance==null){
synchronized (ActManager.class){
if (mInstance==null){
mInstance=new ActManager();
}
}
}
return mInstance;
}
public void setAct(Activity act){
mReference=new WeakReference<Activity>(act);
}
public void startAct(Context context){
Intent intent = new Intent(context.getApplicationContext(),EmptyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public void finishAct(){
if (mReference!=null&&mReference.get()!=null){
mReference.get().finish();
}
}
}
封裝一個鎖屏和解鎖的廣播接收者
public class ScreenLIstener {
private Callback mCallback;
private Context mContext;
ScreenListenerBrocastReceiver mScreenReceiver;
public ScreenLIstener(Context context,Callback callback) {
mContext=context;
mCallback = callback;
mScreenReceiver=new ScreenListenerBrocastReceiver();
register();
}
private class ScreenListenerBrocastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case Intent.ACTION_SCREEN_ON:
if (mCallback!=null){
mCallback.onScreenOn();
}
break;
case Intent.ACTION_SCREEN_OFF:
if (mCallback!=null){
mCallback.onScreenOff();
}
break;
}
}
}
private void register() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(mScreenReceiver, filter);
}
public void unregister(){
mContext.unregisterReceiver(mScreenReceiver);
}
public interface Callback{
void onScreenOn();
void onScreenOff();
}
}
開啓一個服務
public class KeepAliveService extends Service {
ScreenLIstener screenLIstener;
@Override
public void onCreate() {
super.onCreate();
screenLIstener=new ScreenLIstener(this, new ScreenLIstener.Callback() {
@Override
public void onScreenOn() {
Log.d("meee",getClass()+":\n"+"開屏幕:");
ActManager.getInstance()
.finishAct();
Toast.makeText(getApplicationContext(),"已解鎖",Toast.LENGTH_SHORT).show();
}
@Override
public void onScreenOff() {
Log.d("meee",getClass()+":\n"+"鎖屏:");
ActManager.getInstance()
.startAct(KeepAliveService.this);
}
});
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}