package com.forfan.bigbang.component.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
/**
* 在锁屏后启动activity,解锁后关闭,可以提高进程的优先级,不被杀死
*/
public class KeepAliveActivity extends AppCompatActivity {
private ScreenBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
receiver = new ScreenBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
try {
registerReceiver(receiver,filter);
} catch (Throwable e) {
}
Window window = getWindow();
//放在左上角
window.setGravity(Gravity.START | Gravity.TOP);
WindowManager.LayoutParams attributes = window.getAttributes();
//宽高设计为1个像素
attributes.width = 1;
attributes.height = 1;
//起始坐标
attributes.x = 0;
attributes.y = 0;
window.setAttributes(attributes);
}
@Override
protected void onDestroy() {
try {
unregisterReceiver(receiver);
} catch (Throwable e) {
}
super.onDestroy();
}
private class ScreenBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) {
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
finish();
}
}
}
}