Android 给App保活(10种方案)

1.Activity 1像素保活

public class Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);

        Window window = getWindow();
        window.setGravity(Gravity.LEFT|Gravity.TOP);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = 1;
        layoutParams.height = 1;
        layoutParams.x = 1;
        layoutParams.y = 1;
        window.setAttributes(layoutParams);
    }
}

2.前台服务

public class ForegroundService extends Service {
    private final static int SERVICE_ID = 1;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN_MR2){
            //4.3以下
            startForeground(SERVICE_ID,new Notification());
        }else if (Build.VERSION.SDK_INT<Build.VERSION_CODES.O){
            //7.0以下
            startForeground(SERVICE_ID,new Notification());
            //删除通知栏
            startService(new Intent(this,InnerService.class));
        }else {
            //8.0以上
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //NotificationManager.IMPORTANCE_MIN 通知栏消息的重要级别  最低,不让弹出
            //IMPORTANCE_MIN 前台时,在阴影区能看到,后台时 阴影区不消失,增加显示 IMPORTANCE_NONE时 一样的提示
            //IMPORTANCE_NONE app在前台没有通知显示,后台时有
            NotificationChannel channel = new NotificationChannel("channel", "keep", NotificationManager.IMPORTANCE_NONE);
            if (notificationManager!=null){
                notificationManager.createNotificationChannel(channel);
                Notification notification = new Notification.Builder(this, "channel").build();
                startForeground(SERVICE_ID,notification);
            }

        }
    }


    private static class InnerService extends Service{
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(KeepAliveApp.TAG, "onCreate: ");
            startForeground(SERVICE_ID,new Notification());
            stopSelf();
        }
    }
}

3.广播拉活

在发生特定系统事件时,系统会发出广播,通过在 AndroidManifest 中静态注册对应的广播监听器,即可在发生响应事件时拉活。

        但是从android 7.0开始,对广播进行了限制,而且在8.0更加严格https://developer.android.google.cn/about/versions/oreo/background.html#broadcasts

        可静态注册广播列表: https://developer.android.google.cn/guide/components/broadcast-exceptions.html

4.利用系统机制拉活

START_STICKY:

    “粘性”。如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。

START_NOT_STICKY:

    “非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。

START_REDELIVER_INTENT:

    重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。

START_STICKY_COMPATIBILITY:

    START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

只要 targetSdkVersion 不小于5,就默认是 START_STICKY。 但是某些ROM 系统不会拉活。并且经过测试,Service 第一次被异常杀死后很快被重启,第二次会比第一次慢,第三次又会比前一次慢,一旦在短时间内 Service 被杀死4-5次,则系统不再拉起。

public class StickyService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

5.账户同步拉活

/**
 * 创建 可添加用户
 */
public class AuthenticationService extends Service {

    private AccountAuthenticator accountAuthenticator;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return accountAuthenticator.getIBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        accountAuthenticator = new AccountAuthenticator(this);
    }

    public static class AccountAuthenticator extends AbstractAccountAuthenticator {

        public AccountAuthenticator(Context context) {
            super(context);
        }

        @Override
        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
            return null;
        }

        @Override
        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
                                 String[] requiredFeatures, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
                                         Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
                                   String authTokenType, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public String getAuthTokenLabel(String authTokenType) {
            return null;
        }

        @Override
        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
                                        String authTokenType, Bundle options) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
                                  String[] features) throws NetworkErrorException {
            return null;
        }
    }
}
public class AccountHelper {
    private static final String TAG = "AccountHelper";

    private static final String ACCOUNT_TYPE = "com.zsl.keepalive.account";
    private static final String AUTHORITY = "com.zsl.keepalive.provider";

    /**
     * 添加账号
     *
     * @param context
     */
    public static void addAccount(Context context) {
        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        // 获得此类型的账户
        // 需要增加权限  GET_ACCOUNTS
        Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
        if (accounts.length > 0) {
            Log.e(TAG, "账户已存在");
            return;
        }
        Account account = new Account("zsl", ACCOUNT_TYPE);
        // 给这个账户类型添加一个账户
        // 需要增加权限  AUTHENTICATE_ACCOUNTS
        accountManager.addAccountExplicitly(account, "keepalive", new Bundle());
    }

    /**
     * 设置账户自动同步
     */
    public static void autoSync() {
        Account account = new Account("zsl", ACCOUNT_TYPE);
        // 下面三个都需要同一个权限  WRITE_SYNC_SETTINGS
        // 设置同步
        ContentResolver.setIsSyncable(account, AUTHORITY, 1);
        // 自动同步
        ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
        // 设置同步周期
        ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 1);
    }
}
public class SyncService extends Service {

    private SyncAdapter mSyncAdapter;

    private static final String TAG = "SyncService";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mSyncAdapter.getSyncAdapterBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mSyncAdapter = new SyncAdapter(getApplicationContext(), true);
    }

    public static class SyncAdapter extends AbstractThreadedSyncAdapter {

        public SyncAdapter(Context context, boolean autoInitialize) {
            super(context, autoInitialize);
        }

        @Override
        public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
            Log.e(TAG, "同步账户");
            //与互联网 或者 本地数据库同步账户
        }
    }
}
public class SyncProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
                        @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
                      @Nullable String[] selectionArgs) {
        return 0;
    }
}

6.JobScheduler拉活

JobScheduler允许在特定状态与特定时间间隔周期执行任务。可以利用它的这个特点完成保活的功能,效果即开启一个定时器,与普通定时器不同的是其调度由系统完成。     同样在某些ROM可能并不能达到需要的效果

public class KeepAliveJobService extends JobService {
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
            startJob(this);
        }
        return false;
    }

    private void startJob(Context context) {
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
        JobInfo.Builder builder = new JobInfo.Builder(27,
                new ComponentName(context.getPackageName(), KeepAliveJobService.class.getName()))
                .setPersisted(true);
        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.N){
            builder.setPeriodic(1000);
        }else {
            builder.setMinimumLatency(1000);
        }
        jobScheduler.schedule(builder.build());

    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }
}

7.双进程守护

AIDL+JobScheduler 实现

// IMyAidlInterface.aidl
package com.zsl.keepalive;

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

interface IMyAidlInterface {

}
public class LocalService extends Service {

    private static final String TAG = "LocalService ";
    private ServiceConnection serviceConnection;
    private static final int SERVICE_ID = 16;
    private MyBinder myBinder;

    class MyBinder extends IMyAidlInterface.Stub {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        myBinder = new MyBinder();
        serviceConnection = new MyServiceConnection();

        // 让服务变成前台服务
        startForeground(SERVICE_ID, new Notification());
        // 如果18以上的设备 使用相同id再次启动一个前台服务 然后结束这个服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            startService(new Intent(this, InnerService.class));
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bindService(new Intent(this, RemoteService.class),
                serviceConnection, BIND_AUTO_CREATE);
        return super.onStartCommand(intent, flags, startId);
    }

    class MyServiceConnection implements ServiceConnection {

        // 服务连接后回调
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }

        // 连接中断后回调
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "RemoteService 可能被杀死了,拉活");

            startService(new Intent(LocalService.this, RemoteService.class));
            bindService(new Intent(LocalService.this, RemoteService.class),
                    serviceConnection, BIND_AUTO_CREATE);
        }
    }

    public static class InnerService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            startForeground(SERVICE_ID, new Notification());
            stopSelf();
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}
public class RemoteService extends Service {

    private static final String TAG = "RemoteService ";
    private ServiceConnection serviceConnection;
    private static final int SERVICE_ID = 16;
    private MyBinder myBinder;
    class MyBinder extends IMyAidlInterface.Stub {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        myBinder = new MyBinder();
        serviceConnection = new MyServiceConnection();

        // 让服务变成前台服务
        startForeground(SERVICE_ID, new Notification());
        // 如果18以上的设备 使用相同id再次启动一个前台服务 然后结束这个服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            startService(new Intent(this, InnerService.class));
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bindService(new Intent(this, LocalService.class),
                serviceConnection, BIND_AUTO_CREATE);
        return super.onStartCommand(intent, flags, startId);
    }

    class MyServiceConnection implements ServiceConnection {

        // 服务连接后回调
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }

        // 连接中断后回调
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "LocalService 可能被杀死了,拉活");

            startService(new Intent(RemoteService.this, LocalService.class));
            bindService(new Intent(RemoteService.this, LocalService.class),
                    serviceConnection, BIND_AUTO_CREATE);
        }
    }

    public static class InnerService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            startForeground(SERVICE_ID, new Notification());
            stopSelf();
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}

 KeepAliveJobService .java

@Override
    public boolean onStartJob(JobParameters params) {
        Log.e(TAG, "onStartJob");

        // 如果7.0以上 轮询
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            startJob(this);
        }
        boolean isLocal = Utils.isRunningService(this, LocalService.class.getName());
        boolean isRemote = Utils.isRunningService(this, RemoteService.class.getName());
        if (!isLocal || !isRemote) {
            startService(new Intent(this, LocalService.class));
            startService(new Intent(this, RemoteService.class));
        }
        return false;
    }
public class Utils {

    public static boolean isRunningService(Context context, String name) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServices = am.getRunningServices(100);
        for (ActivityManager.RunningServiceInfo info : runningServices) {
            if (TextUtils.equals(info.service.getClassName(), name)) {
                return true;
            }
        }
        return false;
    }

}

8.播放无声音乐

9.WorkManager

public class KeepLiveWork extends Worker {

    public KeepLiveWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.e("KeepAlive", "doWork: ");

        KeepAliveJobService.startJob(getApplicationContext());

        return Result.success();
    }
}

添加任务 

 OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest
                .Builder(KeepLiveWork.class)
                .setInitialDelay(10, TimeUnit.SECONDS)
                .build();

        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);

10.厂商白名单

终极保活,需要厂商定制

  • 4
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Android 9(Pie)中,应用程序保活的机制被加强了,以提高设备的电池寿命和性能。以下是一些建议,可以帮助您在Android 9上保活应用程序: 1. 使用JobScheduler API:使用JobScheduler API可以在特定的时间间隔内执行后台任务,并在系统处于活动状态时保持应用程序保活。JobScheduler API允许您设置不同的约束条件,如设备处于充电状态、网络可用等。 2. 前台服务:将应用程序转换为前台服务可防止系统在内存紧张时杀死应用程序。前台服务会在状态栏中显示一个通知,并且用户可以通过点击通知来打开应用程序。 3. 后台限制:Android 9引入了一新的后台限制机制,以防止应用程序在后台执行消耗电量的操作。如果您的应用程序需要在后台执行某些操作,请确保您的应用程序符合后台限制规则。 4. 使用WorkManager API:WorkManager API是一新的后台任务管理API,它可以自动选择最佳的执行方式来执行后台任务,包括JobScheduler、AlarmManager、Firebase JobDispatcher等。 5. 使用Doze模式:Doze模式是一省电模式,它会在设备处于空闲状态时限制应用程序的后台操作。如果您的应用程序不需要在后台执行实时任务,可以使用Doze模式以延长设备的电池寿命。 请注意,Android 9中的应用程序保活机制可能会因设备制造商和运营商的定制而有所不同。因此,您需要仔细测试您的应用程序,并确保它在各设备和Android版本上都能正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值