基本概念
进程的优先级
安卓系统定义了了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");
}
}