Android安卓进程保活(一)1像素且透明Activity
【摘要】 Android进程保活·1像素且透明Activity提升App进程优先级
Android进程
此文章代码Github上有提交:https://github.com/NorthernBrain/processKeep_Activity
其它文章
Android安卓进程保活(一)1像素且透明Activity
Android安卓进程保活(二)设置前台S...
Android进程保活·1像素且透明Activity提升App进程优先级
Android进程
此文章代码Github上有提交:https://github.com/NorthernBrain/processKeep_Activity
其它文章
Android安卓进程保活(一)1像素且透明ActivityAndroid安卓进程保活(二)设置前台Service
Android安卓进程保活(三)双进程拉活(Java层)
- 前台进程 (Foreground process)
- 可见进程 (Visible process)
- 服务进程 (Service process)
- 后台进程 (Background process)
- 空进程 (Empty process)
下面进行解释:
前台进程(Foreground process):
- 托管用户正在交互的 Activity(已调用 Activity 的 onResume() 方法)
- 托管某个 Service,后者绑定到用户正在交互的 Activity
- 托管正在“前台”运行的 Service(服务已调用 startForeground())
- 托管正执行一个生命周期回调的 Service(onCreate()、onStart() 或 onDestroy())
- 托管正执行其 onReceive() 方法的 BroadcastReceiver
可见进程 (Visible process):
- 托管不在前台、但仍对用户可见的 Activity(已调用其 onPause() 方法)。例如,如果前台 Activity 启动了一个对话框,允许在其后显示上一 Activity,则有可能会发生这种情况。
- 托管绑定到可见(或前台)Activity 的 Service。
可见进程被视为是极其重要的进程,除非为了维持所有前台进程同时运行而必须终止,否则系统不会终止这些进程。
服务进程 (Service process):
后台进程 (Service process):
空进程 (Empty process):
进程优先级:
Android进程保活
1像素且透明Activity提升App进程优先级 通过设置前台Service提升App进程优先级 Java层的双进程拉活 JobScheduler实现 NDK双进程守护 使用账户同步拉活 workmanager实现- 红色部分是容易被回收的进程,属于android进程
- 绿色部分是较难被回收的进程,属于android进程
- 其他部分则不是android进程,也不会被系统回收,一般是ROM自带的app和服务才能拥有
本篇文章介绍的是进程第一种方式:
- 1像素且透明Activity提升App进程优先级
1像素且透明Activity提升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"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
<style name="KeepLiveTheme" parent="AppTheme"> <item name="android:windowBackground">@null</item> <item name="android:windowIsTranslucent">true</item>
</style>
- 1
- 2
- 3
- 4
<activity android:name=".KeepLiveActivity" android:excludeFromRecents="true" android:exported="false" android:finishOnTaskLaunch="false" android:launchMode="singleInstance" android:theme="@style/KeepLiveTheme"/>
- 1
- 2
- 3
- 4
- 5
- 6
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); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
/*
* 广播接收者监听屏幕开启和熄灭
* */
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(); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
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); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
文章来源: blog.csdn.net,作者:第三女神程忆难,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_40881680/article/details/85120297
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)