Android --(Service)AIDL跨线程通信

Android AIDL详解与跨线程通信实践
本文介绍了Android中AIDL(Android Interface Definition Language)的详细信息,阐述了其作为多应用间Service通信手段的优势,如性能稳定和效率高。通过步骤展示了如何在Service端创建AIDL接口,包括创建aidl文件并重建,以及如何创建Service。客户端的创建过程也进行了说明,特别是第三方应用如何调用该服务,包括复制AIDL文件到第三方应用以及创建Component。

一,AIDL详情

AIDL是一种定义接口语言,用于多个应用间使用同一个Service的功能。

相比其他跨线程通信:Broadcast Receiver,它的优势是:性能稳定,效率高,而且系统上实现共享内存。

二,AIDL的使用:

通常使用它需要启动Client端和Service端

Service端创建

1,先创建AIDL

 

2,修改aidl文件,并 Rebuild

interface IMyAidlInterface {
    //传入int数据,返回string 数据
    String getstring(int data);
}

 

3,创建service

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

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

    private IMyAidlInterface.Stub iMyAidlInterface = new IMyAidlInterface.Stub() {
        @Override
        public String getstring(int data) throws RemoteException {
            String str = "传进来的数字:"+data;
            return str;
        }
    };
}

Client 客户端创建

  private  IMyAidlInterface iMyAidlInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * 绑定AIDL
     */
    public void onbang(View view ){
        Intent intent = new Intent(this , MyService.class);
        bindService(intent , connect , BIND_AUTO_CREATE);

    }

    /**
     * 解绑
     */
    public void unbang(View view){
        unbindService(connect);
        Toast.makeText(MainActivity.this , "解除绑定" , Toast.LENGTH_LONG).show();
    }

    /**
     * 连接
     */
    private ServiceConnection  connect = new ServiceConnection() {
        /**
         * 连接成功
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                String str = iMyAidlInterface.getstring(1);
                Toast.makeText(MainActivity.this , str , Toast.LENGTH_LONG).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        /**
         * 连接失败
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            iMyAidlInterface = null;

        }
    };
}

显示如图

第三方应用调用该服务

这里和之前的不一样

1,把之前的ADIL文件夹复制到第三方应用

2,调取的时候发生改变,创建Componnent

   public void onbang(View view ){
        Intent intent = new Intent();
        ComponentName com = new ComponentName(
                "qzjiami.c",  //包名
                "qzjiami.c.MyService");//服务类
        intent.setComponent(com);
        bindService(intent , connect , BIND_AUTO_CREATE);

    }

主代码:

public class MainActivity extends AppCompatActivity {
    private IMyAidlInterface DiMyAidlInterface;

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

    /**
     * 绑定AIDL
     */
    public void onbang(View view ){
        Intent intent = new Intent();
        ComponentName com = new ComponentName(
                "qzjiami.c",  //包名
                "qzjiami.c.MyService");//服务类
        intent.setComponent(com);
        bindService(intent , connect , BIND_AUTO_CREATE);

    }

    /**
     * 解绑
     */
    public void unbang(View view){
        unbindService(connect);
        Toast.makeText(MainActivity.this , "解除绑定" , Toast.LENGTH_LONG).show();
    }

    /**
     * 连接
     */
    private ServiceConnection connect = new ServiceConnection() {
        /**
         * 连接成功
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            DiMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                String str = DiMyAidlInterface.getstring(2);
                Toast.makeText(MainActivity.this , str , Toast.LENGTH_LONG).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        /**
         * 连接失败
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            DiMyAidlInterface = null;

        }
    };
}

显示如图:

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值