大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  •  世界的顶端
  • 混口饭吃
  • 写了333,609字

最近回复

该文章投稿至Nemo社区   Android  板块 复制链接


Android IPC通讯——AIDL

发布于 2018/09/27 15:58 17,141浏览 0回复 15,905

一. AIDL是什么?

   AIDL是一个缩写,全称是Android Interface Definition Language,也就是Android接口定义语言。

二.AIDL能干什么?

  进行跨进程通信,比如两个APP之间的通讯。

三.AIDL支持的数据类型?

   Java中的八种基本数据类型,String 类型,CharSequence类型,List类型,Map类型,定向tag

四.AIDL的使用

   服务端(APP1):

  1.定义aidl文件,并且rebuild项目

// IMyAidlInterface.aidl
package com.hangzhou.hz890.aidl;

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

interface IMyAidlInterface {
  String getName(); // 客户端获取的数据

  void setName(String name); // 客户端传入的数据
}

2. 创建server

package com.hangzhou.hz890.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       return new MyBinder();
    }

    class MyBinder extends IMyAidlInterface.Stub{

        @Override
        public String getName() throws RemoteException {
            return "服务器返回的数据";
        }

        @Override
        public void setName(String name) throws RemoteException {
            Log.e("xxx:",name);
        }
    }

}

    android:name=".MyService"
android:enabled="true"
android:exported="true">

android:name="com.hangzhou.hz890.server"/>
android:name="android.intent.category.DEFAULT"/>

2.客户端(APP2) 需要将服务端的aidl文件拷贝到客户端,并且保持包名一致

客户端相对简单,只需要绑定服务就可以通讯了

package com.hangzhou.hz890.client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hangzhou.hz890.aidl.IMyAidlInterface;

import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {
    IMyAidlInterface iMyAidlInterface;

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

        // 绑定服务端的服务
        Intent intent = new Intent();
        intent.setAction("com.hangzhou.hz890.server");
        intent.setPackage("com.hangzhou.hz890.aidl");
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        findViewById(R.id.bt_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Toast.makeText(getApplicationContext(), iMyAidlInterface.getName(), Toast.LENGTH_LONG).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.bt_push).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iMyAidlInterface.setName("客户端传输数据给服务端");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };


    @Override
    protected void onStop() {
        super.onStop();
        unbindService(serviceConnection);
    }
}

DEMO:https://github.com/tzz2015/AIDL

四. AIDL的通讯原理

点赞(0)
点了个评