学习笔记之保活技能之1像素Acitivity保活

本文介绍了一种Android应用保活技术——1像素法。通过创建1像素大小的透明Activity并在屏幕角落显示,即使在后台也能维持应用运行状态。文章详细阐述了如何实现该功能,包括创建KeepActivity和KeepManager类、注册广播接收器等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习Android初中级升高级Android工程师中,记录写3中APP保活的方法。

方法1:1像素法

实现原理就是在屏幕总创建1像素透明activity,通过这个activity来实现保活。

1):创建KeepActivity并继承Activity,然后通过Window设置1像素Activity的放置位置

Window window = getWindow();
//把1寸Activity放置左上角
window.setGravity(Gravity.START | Gravity.TOP);

通过WindowManager.LayoutParams设置1像素Activity的大小

WindowManager.LayoutParams wl = window.getAttributes();
wl.width = 1;
wl.height = 1;
wl.x = 0;
wl.y = 0;

window.setAttributes(wl);
2):创建KeepManager,并实现单例模式
private static final KeepManager ourInstance = new KeepManager();

/**
 * 单例模式
 */
public static KeepManager getInstance() {
    return ourInstance;
}

public KeepManager() {

}

实现广播注册开屏、关屏和注销

/**
 * 注册 关屏和开屏
 */
public void registerKeep(Context context) {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    keepReceiver = new KeepReceiver();
    context.registerReceiver(keepReceiver, filter);
}

/**
 * 注销
 */
public void unregisterKeep(Context context) {
    if (keepReceiver != null) {
        context.unregisterReceiver(keepReceiver);
    }
}

实现1像素Activity的绑定和注销方法

/**
 * 开启 1px Acitivity
 */
public void startKeep(Context context) {
    Intent intent = new Intent(context, KeepActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

/**
 * 注销 1px Activity
 */
public void finishKeep() {
    if (mKeepAct != null) {
        Activity activity = mKeepAct.get();
        if (activity != null) {
            activity.finish();
        }
        mKeepAct = null;
    }
}

3):创建KeepReceiver广播,并实现开关屏是的处理逻辑

String action = intent.getAction();
if (TextUtils.equals(action, Intent.ACTION_SCREEN_OFF)) {
    //关屏
    KeepManager.getInstance().startKeep(context);

} else if (TextUtils.equals(action, Intent.ACTION_SCREEN_ON)) {
    //开屏
    KeepManager.getInstance().finishKeep();
}

4)在KeepManager中实现弱引用绑定

private static WeakReference<Activity> mKeepAct;
/**
 * 绑定弱引用
 */
public void setKeep(KeepActivity keep) {
    mKeepAct = new WeakReference<Activity>(keep);
}

5)最后在MainActivity中启用

//通过 1pxActivity 进行提权
KeepManager.getInstance().registerKeep(this);
@Override
protected void onDestroy() {
    super.onDestroy();
    KeepManager.getInstance().unregisterKeep(this);
}

最后记得在清单文件中给1像素Activity做相应的配置,

<activity
    android:name=".activity.KeepActivity"
    android:excludeFromRecents="true"
    android:taskAffinity="com.pan.appkeeplive"
    android:theme="@style/KeepTheme" /> //设置主题

在style中的keepTheme

<style name="KeepTheme">
    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

panyingdao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值