Android学习之鸿蒙&Android类型技术总结

Android中的保活机制

2021-06-16  本文已影响0人  乌托邦式的爱情

Android中的保活是一个永不过时的话题,因为每一个APP都希望能在后台不停的运行去搜集用户数据,在Android 系统处于较低版本的时候(目前最新版本为12,较低版本指的是8以下),很多APP借助于系统层面的漏洞研发出了各种保活的方法,但是随着Android 版本的不断更新,过往的保活方法渐渐失效,Android中的保活成为了一个越来越难办到的事情,但是我认为这是一个好事,如非这样你永远不知道你的手机后台到底有多少APP背着你干了多少事情。当然,系统的事情不是我们能掌控的了的。那么,我们先来看看以前为了保活都有哪些手段。

手段一:在Service的onStartCommand方法中返回****START_STICKY****(****亲测无效****)

在Service的onStartCommand方法中返回键有下面几种可供选择:

(1)START_STICKY:如果Service所在的进程,在执行了onStartCommand方法后,被清理了,那么这个Service会被保留在已开始的状态,但是不保留传入的Intent,随后系统会尝试重新创建此Service。

(2)START_NOT_STICKY:如果Service所在的进程,在执行了onStartCommand方法后,被清理了,则系统不会重新启动此Service。

(3)START_REDELIVER_INTENT:如果Service所在的进程,在执行了onStartCommand方法后,被清理了,则结果和START_STICKY一样,也会重新创建此Service并调用onStartCommand方法。不同之处在于,如果是返回的是START_REDELIVER_INTENT ,则重新创建Service时onStartCommand方法会传入之前的intent。

手段二:在清单文件里面设置优先级****(****亲测无效****)

1.png

手段三:在Service即将销毁的时候重新启动****(****亲测无效****)

2.png

手段四:借助AIDL使用双进程保活****(****亲测无效****)

首先创建一个AIDL文件


package com.steven.activitydemo;

// Declare any non-default types here with import statements

interface ProcessConnection {

 String getServiceName();

}

创建本地服务


public class LocalService extends Service {
    private MyBinder mBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ProcessConnection iMyAidlInterface = ProcessConnection.Stub.asInterface(service);
            try {
                Log.i("LocalService", "connected with " + iMyAidlInterface.getServiceName());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("stevens", "链接断开,重新启动 RemoteService");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(new Intent(LocalService.this, RemoteService.class));
            } else {
                startService(new Intent(LocalService.this, RemoteService.class));
            }
            bindService(new Intent(LocalService.this, RemoteService.class), connection, Context.BIND_IMPORTANT);
        }
    };

    public LocalService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TimeTool.getInstance();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("stevens", "LocalService 启动");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(new Intent(LocalService.this, RemoteService.class));
        } else {
            startService(new Intent(LocalService.this, RemoteService.class));
        }
        bindService(new Intent(this, RemoteService.class), connection, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        Log.i("stevens", "-----local onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        mBinder = new MyBinder();
        return mBinder;
    }

    private class MyBinder extends ProcessConnection.Stub {

        @Override
        public String getServiceName() throws RemoteException {
            return LocalService.class.getName();
        }
    }
}

创建远程服务:

public class RemoteService extends Service {

    private MyBinder mBinder;


    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ProcessConnection iMyAidlInterface = ProcessConnection.Stub.asInterface(service);
            try {
                Log.i("RemoteService", "connected with " + iMyAidlInterface.getServiceName());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("stevens", "链接断开,重新启动 LocalService");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(new Intent(RemoteService.this, LocalService.class));
            } else {
                startService(new Intent(RemoteService.this, LocalService.class));
            }
            bindService(new Intent(RemoteService.this, LocalService.class), connection, Context.BIND_IMPORTANT);
        }
    };

    public RemoteService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("stevens", "RemoteService 启动");
        bindService(new Intent(this, LocalService.class), connection, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        Log.i("stevens", "-----remote onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        mBinder = new MyBinder();
        return mBinder;
    }

    private class MyBinder extends ProcessConnection.Stub {

        @Override
        public String getServiceName() throws RemoteException {
            return RemoteService.class.getName();
        }
    }
}

最后在清单文件声明:

<service
    android:name=".doubleservice.RemoteService"
    android:enabled="true"
    android:exported="true"
    android:process=":RemoteProcess" />
<service
    android:name=".doubleservice.LocalService"
    android:enabled="true"
    android:exported="true" />

手段五:1像素的Activity让应用在熄屏后保活****(****亲测无效****)

具体怎么实现可以参照这篇文章

<u>https://blog.csdn.net/zhenufo/article/details/79317068</u>

运行一段时间后系统会自动杀死整个APP

手段六:****开启前台服务(亲测有效)

在Service的onCreate方法中开启前台服务

3.png

当然,APP保活的方式方法远不止这些,但是随着Android 系统的不断优化,保活现在越来越不太现实,但是我们可以尽量提高我们APP的优先级让系统不轻易杀死我们的APP,这一点还是可以办到的。

上一篇 下一篇

猜你喜欢

热点阅读