Android中AIDL的使用

AIDL说明

AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写,对于小白来说,AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的这个应用可以和其他应用交互,也就是应用之间的通信

AIDL的用法

首先创建服务端,我这里创建一个名为appservice的Module 右键创建aidl文件如下图


我这里就用系统默认生成的名字IMyAidlInterface(可以自己随意命名)

// IMyAidlInterface.aidl
package t.s.com.appservice;

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

interface IMyAidlInterface {

     String getMsg();//我自己定义的方法
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);//basicTypes这个方法可以无视,看注解知道这个方法只是告诉你在AIDL中你可以使用的基本类型(int, long, boolean, float, double, String)
}
定义好之后,就可以sycn project一下,然后新建一个service。在service里面创建一个内部类,继承你刚才创建的AIDL的名称里的Stub类,并实现接口方法,在onBind返回内部类的实例。
package t.s.com.appservice;

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

public class MyService extends Service {
    public void onCreate() {
        super.onCreate();
       Log.i("tttt","onCreate");
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return myAidlInterface;
    }


    private final IMyAidlInterface.Stub myAidlInterface= new IMyAidlInterface.Stub() {
        @Override
        public String getMsg() throws RemoteException {
            Log.i("tttt","getMsg");
            String msg="我的AIDL";
            return msg;
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
            Log.i("tttt","basicTypes");
        }
    };
}
记得在Mainifest.xml注册这个service
        <service
            android:name=".MyService">
            <intent-filter>
                <action android:name="t.s.com.appservice" />
            </intent-filter>
        </service>

到此整个服务端的就创建完毕!

下面看下客服端如何调用

将我们刚刚服务端的AIDL文件拷贝到第二个项目(注意两个aidl文件所在路径要完全一样,看下图)



然后sycn project一下工程。

然后在Activity中绑定服务

package t.s.com;

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

import butterknife.ButterKnife;
import butterknife.OnClick;
import t.s.com.appservice.IMyAidlInterface;
import t.s.com.desgin.DesignActivity;
import t.s.com.desgin.DrawerLayoutActivity;
import t.s.com.javaee.ProductList;
import t.s.com.javaee.ProductTwo;
import t.s.com.javaee.Productone;


public class MainActivity extends AppCompatActivity {
    private IMyAidlInterface maidl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ProductList productList=ProductList.getInstance();
        Productone pone=new Productone();
        ProductTwo ptwo=new ProductTwo();
        productList.addObserver(pone);
        productList.addObserver(ptwo);
        productList.addProuduct("新进产品");

    }


    @OnClick({R.id.one, R.id.two, R.id.three,R.id.aidls,R.id.detail})
    void onclick(View v) {
        switch (v.getId()) {
            case R.id.one:
                startActivity(new Intent(MainActivity.this, DrawerLayoutActivity.class));
                break;
            case R.id.two:
                startActivity(new Intent(MainActivity.this, DesignActivity.class));
                break;
            case R.id.three:
                startActivity(new Intent(MainActivity.this, DesignActivity.class));
                break;
            case R.id.aidls://绑定
                buttonClick();
                break;
            case R.id.detail://调用方法
                if(maidl==null){
                    buttonClick();
                }else {
                    String sd="";
                    try {
                        sd=maidl.getMsg();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(MainActivity.this,"lll="+sd ,Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    /**
     * 监听按钮点击
     * @param
     */
    public void buttonClick() {
        Log.i("tttt","buttonClick=");
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("t.s.com.appservice", "t.s.com.appservice.MyService"));//(服务端service所在包名,服务端service全路径)
        bindService(intent, conn, Context.BIND_AUTO_CREATE);

    }

    ServiceConnection conn = new ServiceConnection(){
        @Override
        public void onServiceDisconnected(ComponentName name){

        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service){
            maidl = IMyAidlInterface.Stub.asInterface(service);
            try {
                Toast.makeText(MainActivity.this,"绑定成功" ,Toast.LENGTH_SHORT).show();
                maidl.basicTypes(12, 1332, true, 12.2f, 12.3, "测试测试");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            Log.i("tttt",maidl.toString());
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}

到此,AIDL的使用方法就完成了

原码地址请点击




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

揽m月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值