上篇博客講了我的背裕活之旅,這篇說一下“1像素頁面迸悴龋活”的具體實現(xiàn)杖们。
第一步,新建一個Activity肩狂,作為1像素頁面的主體摘完,我姑且叫它HooliganActivity:
public class HooliganActivity extends Activity {
private static HooliganActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
Window window = getWindow();
window.setGravity(Gravity.LEFT | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.height = 1;
params.width = 1;
window.setAttributes(params);
}
/**
* 開啟保活頁面
*/
public static void startHooligan() {
Intent intent = new Intent(DWApplication.getAppContext(), HooliganActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DWApplication.getAppContext().startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
instance = null;
}
/**
* 關(guān)閉鄙邓活頁面
*/
public static void killHooligan() {
if(instance != null) {
instance.finish();
}
}
}
第二步孝治,注冊清單文件:
<activity android:name=".activity.HooliganActivity"
android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:theme="@style/HooliganActivityStyle"/>
<style name="HooliganActivityStyle">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoDisplay">false</item>
<item name="android:windowDisablePreview">true</item>
</style>
第三步,監(jiān)聽鎖屏和解鎖通知审磁,不能靜態(tài)注冊廣播谈飒,只能動態(tài)注冊:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BootCompleteReceiver(),filter);
第四步,分別在解鎖和鎖屏?xí)r喚醒我的HooliganActivity:
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
HooliganActivity. startHooligan();
} else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
HooliganActivity. killHooligan();
}
}
}
這樣你在后臺每次鎖屏态蒂,實際上都會吊起一個一像素的頁面杭措,假裝app在前臺,擁有最高進(jìn)程優(yōu)先級钾恢。
??不推薦使用手素,太流氓了- -!瘩蚪。
技術(shù)無罪泉懦。
?????????????????????????????? ——王欣
但人有罪。