双进程保活加隐式启动

记得使用AIDL定义接口完成进程间Service的通信

清单文件

 <application>
        <service
            android:name=".service.RemoteService"
            android:process="com.example.remote"//设置为一个进程里面写进程名字
            android:enabled="true"
            android:exported="true">
            <intent-filter>//隐式启动
                <action android:name="com.example.myappliucation.REMOTE" />
            </intent-filter>
        </service>
        <service
            android:name=".service.JournalismService"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.example.myappliucation.JOURNALISM" />
            </intent-filter>
        </service>
        <service android:name=".service.JournalismService$InnerService" />
    </application>

Activity

  		Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
  		//加上启动的路径
        intent.setPackage("com.example.journalism");
        startService(intent);
        Intent remote = new Intent("com.example.myappliucation.REMOTE");
        remote.setPackage("com.example.journalism");
        startService(remote);

服务

package com.example.formwork.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.formwork.IMyAidlInterface;
import com.example.formwork.R;

public class JournalismService extends Service {
    private static final String TAG = "JournalismService";

    public JournalismService() {
    }

    private class JournalismBinder extends IMyAidlInterface.Stub {
        @Override
        public String getString() throws RemoteException {
            return RemoteService.class.getSimpleName();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new JournalismService.JournalismBinder();
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);

            try {
                Log.d( "JournalismService",""+iMyAidlInterface.getString());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {//当绑定的RemoteService被系统回收后,该回调函数将被调用
            Log.d( "JournalismService","JournalismService被系统回收,回调函数被执行");
            Intent intent = new Intent("com.example.myappliucation.REMOTE");
            intent.setPackage("com.example.journalism");
            startService(intent);
            bindService(intent, serviceConnection, BIND_ABOVE_CLIENT);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent("com.example.myappliucation.REMOTE");
        intent.setPackage("com.example.journalism");
        startService(intent);
        bindService(intent, serviceConnection, BIND_ABOVE_CLIENT);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            Log.i(TAG, "onStartCommand: " + intent.getStringExtra("test"));
        } else {
            Log.i(TAG, "onStartCommand: ");
        }
        super.onStartCommand(intent, flags, startId);

        //前台通知保活 保活之后划掉时候内存充足,进程不会被杀死,再次打开是热启动
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.night_mode);
        builder.setContentTitle("今日头条正在运行...");
        //正常通知
//        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        manager.notify(22,builder.build());
        //前台通知  提升优先级保活
        startForeground(22, builder.build());
        Intent service = new Intent(this, InnerService.class);
        startService(service);


//        //销毁进程之后如果内存充足再次启动保活进程可以获取到intent
//        return START_REDELIVER_INTENT;
//        //销毁进程之不启动
//        return START_NOT_STICKY;
        //销毁进程之后如果内存充足再次启动保活进程不能获取到intent
        return START_STICKY;
    }


    public static class InnerService extends Service {

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

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            startForeground(22, new Notification());
            //关闭自己
            stopSelf();
            return START_STICKY;
        }
    }
}

保活服务

package com.example.formwork.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.formwork.IMyAidlInterface;

public class RemoteService extends Service {
    public RemoteService() {
    }

    private class RemoteBinder extends IMyAidlInterface.Stub {
        @Override
        public String getString() throws RemoteException {
            return RemoteService.class.getSimpleName();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new RemoteBinder();
    }


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

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);

            try {
                Log.d( "RemoteService",""+iMyAidlInterface.getString());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {//当绑定的RemoteService被系统回收后,该回调函数将被调用
            Log.d( "RemoteService","RemoteService被系统回收,回调函数被执行");
            Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
            intent.setPackage("com.example.journalism");
            startService(intent);
            bindService(intent,serviceConnection , BIND_ABOVE_CLIENT);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        //启动连接
        Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
        intent.setPackage("com.example.journalism");
        startService(intent);
        bindService(intent,serviceConnection , BIND_ABOVE_CLIENT);
    }
}

adb 命令

adb -s emulator-5554 shell ps  //查询 emulator-5554 这个设备所有的进程
adb -s emulator-5554 shell kill 6875  //查询 emulator-5554 这个设备 6875 进程
adb devices  //查询所有设备
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值