Android之进程保活

清单文件

        <service
            android:name=".doubleservice.TwoService"
            android:process="headlinenews.two"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.two"></action>
            </intent-filter>
        </service>
        <service
            android:name=".doubleservice.OneService"
            android:process="headlinenews.one"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.one"></action>
            </intent-filter>
        </service>

创建aidl文件

interface IMyAidlInterface {
    String getName();
    void getTime();
}

One

package com.example.headlinenews.doubleservice;

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.headlinenews.IMyAidlInterface;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("zyb", "OneService onCreate: ");
        Intent intent = new Intent();
        intent.setAction("com.example.two");
        intent.setPackage("com.example.headlinenews");
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }

    @Override
    public IBinder onBind(Intent intent) {

        return new OneBinder();
    }

    private class OneBinder extends IMyAidlInterface.Stub{

        @Override
        public String getName() throws RemoteException {
            return "one";
        }

        @Override
        public void getTime() throws RemoteException {
            Log.d("zyb", "OneBinder getTime: ");

        }
    }
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                Log.d("zyb", "onServiceConnected: one"+iMyAidlInterface.getName());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("zyb", "OneService onServiceDisconnected: ");
            Intent intent = new Intent();
            intent.setAction("com.example.two");
            intent.setPackage("com.example.headlinenews");
            startService(intent);
            bindService(intent,serviceConnection,BIND_AUTO_CREATE);
        }
    };
}


Two

package com.example.headlinenews.doubleservice;

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.headlinenews.IMyAidlInterface;
import com.example.headlinenews.service.ProgressService;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("zyb", "TwoService onCreate: ");
        Intent intent = new Intent();
        intent.setAction("com.example.one");
        intent.setPackage("com.example.headlinenews");
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }

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

    private class TwoBinder extends IMyAidlInterface.Stub{

        @Override
        public String getName() throws RemoteException {
            Log.d("zyb", "TwoBinder getName: ");
            return "two";
        }

        @Override
        public void getTime() throws RemoteException {
            Log.d("zyb", "TwoBinder getTime: ");
        }
    }
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                Log.d("zyb", "onServiceConnected: two"+iMyAidlInterface.getName());

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Intent intent = new Intent("com.example.one");
            intent.setPackage("com.example.headlinenews");
            bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            startService(intent);
            Log.d("zyb", "TwoService onServiceDisconnected: ");

        }
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值