Android跨进程启动Service并利用AIDL通信

1.在服务端和客户端各创建aidl文件,保证其包名完全一致。
在这里插入图片描述

2.IMyAidlInterface实现

// IMyAidlInterface.aidl
package com.replace.demo.aidl;
//需要手动引入包名
import com.replace.demo.aidl.IMyAidlInterfaceListener;

interface IMyAidlInterface {
    //发送消息
    void send(String str);
    //获取消息
    void getMessage(IMyAidlInterfaceListener listener);
}

3.IMyAidlInterfaceListener实现

// IMyAidlInterfaceListener.aidl
package com.replace.demo.aidl;
interface IMyAidlInterfaceListener {
    //回调接口
    void onResult(String str);
}

4.服务端实现

public class AidlService extends Service {

    private IMyAidlInterfaceListener iMyAidlInterfaceListener;

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

    @Override
    public void onCreate() {
        super.onCreate();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("HGG_10086"
                    , this.getClass().getName(), NotificationManager.IMPORTANCE_LOW);
            NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (systemService != null) {
                systemService.createNotificationChannel(notificationChannel);
            }
            Notification notification = new Notification.Builder(this, "HGG_10086").build();
            this.startForeground(100816, notification);
        } else {
            this.startForeground(100816, new Notification());
        }
        Log.e("HGG", "接收到:onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("HGG", "接收到:onStartCommand");
        Toast.makeText(this, "接收到:onStartCommand", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }

    public class M extends IMyAidlInterface.Stub {

        @Override
        public void send(String str) throws RemoteException {
            Log.e("HGG", "接收到:" + str);
            if (iMyAidlInterfaceListener != null) {
                iMyAidlInterfaceListener.onResult("接收到:" + str + " 返回:CDD");
            }
        }

        @Override
        public void getMessage(IMyAidlInterfaceListener listener) throws RemoteException {
            iMyAidlInterfaceListener = listener;
        }

    }
}

5.在AndroidManifest中注册Service

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<service
            android:name=".AidlService"
            android:exported="true"> //允许该服务跨进程使用

6.客户端实现

public class AidlActivity extends AppCompatActivity {

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

    private IMyAidlInterface iMyAidlInterface;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("HGG", "onServiceConnected");
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                iMyAidlInterface.getMessage(new IMyAidlInterfaceListener.Stub() {
                    @Override
                    public void onResult(String str) throws RemoteException {
                        Toast.makeText(AidlActivity.this, str, Toast.LENGTH_LONG).show();
                    }
                });
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("HGG", "onServiceDisconnected");
        }
    };

    public void onClick(View view) {
        try {
            if (iMyAidlInterface != null) {
                iMyAidlInterface.send("HGG发送的一个通信");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void onClick2(View view) {
        Intent intent = new Intent();
        //通过包名和类名的完整路劲启动服务
        intent.setClassName("com.example.testaidl", "com.example.testaidl.AidlService");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值