赞
踩
下面进行解释:
可见进程被视为是极其重要的进程,除非为了维持所有前台进程同时运行而必须终止,否则系统不会终止这些进程。
本篇文章介绍的是进程第一种方式:
首先创建KeepLiveActivity.java继承自AppCompatActivity,这就是透明的Activity:↓
public class KeepLiveActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("KeepLiveActivity","开启KeepLiveActivity"); //左上角显示 Window window = getWindow(); window.setGravity(Gravity.START|Gravity.TOP); //设置为1像素大小 WindowManager.LayoutParams params = window.getAttributes(); params.x = 0; params.y = 0; params.width = 1; params.height = 1; window.setAttributes(params); KeepLiveManager.getInstance().setKeepLiveActivity(this); } @Override protected void onDestroy() { super.onDestroy(); Log.d("KeepLiveActivity","关闭KeepLiveActivity"); } }
<style name="KeepLiveTheme" parent="AppTheme">
<item name="android:windowBackground">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<activity android:name=".KeepLiveActivity"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:theme="@style/KeepLiveTheme"/>
public class KeepLiveManager { private static final KeepLiveManager ourInstance = new KeepLiveManager(); public static KeepLiveManager getInstance() { return ourInstance; } private KeepLiveManager() { } //弱引用,防止内存泄漏 private WeakReference<KeepLiveActivity> reference; private KeepLiveReceiver receiver; public void setKeepLiveActivity(KeepLiveActivity activity) { reference = new WeakReference<>(activity); } //开启透明Activity public void startKeepLiveActivity(Context context) { Intent intent = new Intent(context, KeepLiveActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } //关闭透明Activity public void finishKeepLiveActivity() { if (reference != null && reference.get() != null) { reference.get().finish(); } } //注册广播 public void registerKeepLiveReceiver(Context context) { receiver = new KeepLiveReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); context.registerReceiver(receiver, filter); } //反注册 public void unregisterKeepLiveReceiver(Context context){ if(receiver != null){ context.unregisterReceiver(receiver); } } }
/* * 广播接收者监听屏幕开启和熄灭 * */ public class KeepLiveReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { //屏幕关闭,开启透明Activity KeepLiveManager.getInstance().startKeepLiveActivity(context); } else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { //屏幕开启,关闭透明Activity KeepLiveManager.getInstance().finishKeepLiveActivity(); } } }
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1像素且透明Activity提升App进程优先级 KeepLiveManager.getInstance().registerKeepLiveReceiver(this); } @Override protected void onDestroy() { super.onDestroy(); //反注册防止内存泄漏 KeepLiveManager.getInstance().unregisterKeepLiveReceiver(this); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。