赞
踩
红色部分是容易被回收的进程,属于android进程
绿色部分是较难被回收的进程,属于android进程
其他部分则不是android进程,也不会被系统回收,一般是ROM自带的app和服务才能拥有
本篇文章介绍的是进程第一种方式:
首先创建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”);
}
}
<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 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();
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
[外链图片转存中…(img-KXt5Tx3Y-1712391711916)]
[外链图片转存中…(img-uPC1tfu0-1712391711917)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。