Android Aidl的使用

在Android Studio 中使用

Android Studio DemoA

一、在main文件夹下创建aidl文件夹(一定是文件夹,而不是包)

blob.png

二、在aidl文件夹下添加一个包(包名最好是和你的项目的包名一样),

 

在此包中创建一个class文件,将后缀名改为aidl,此文件中的方法是供另一个工程调用的。

blob.png

三、创建一个执行aidl文件中执行声明的方法类

 

此类所继承的 IPerson.Stub 类,就是通过编译aidl文件所生成的(可以理解成通过aidl文件生成了一个抽象类)

所以要重写aidl文件中声明的方法

public class Person extends IPerson.Stub{

    private String name;

 

    @Override

    public void setValue(String name) throws RemoteException {

        this.name = name ;

    }

 

    @Override

    public String getValue() throws RemoteException {

        return name;

    }

}

三、写一个服务,并且在Manifest文件中声明,为该服务添加一个action

public class ServiceC extends Service{

    private IPerson.Stub iPerson = new Person();

    @Override

    public IBinder onBind(Intent arg0) {

        return iPerson;

    }

}

?

<service android:name=".ServiceC">

    <intent-filter>

         </action>

    </intent-filter>

</service>

四、在Activity中绑定开启服务

public class AIDLTestActivity extends AppCompatActivity {

    private IPerson iPerson;

    private ServiceConnection conn = new ServiceConnection() {

 

        // 断开连接时调用

        @Override

        public void onServiceDisconnected(ComponentName arg0) {

        }

        // 连接时调用

        @Override

        public void onServiceConnected(ComponentName arg0, IBinder binder) {

            iPerson = IPerson.Stub.asInterface(binder);

            if (iPerson != null) {

                try {

                    iPerson.setValue("Service AIDL");

                    Toast.makeText(AIDLTestActivity.this, "赋值成功!", Toast.LENGTH_LONG).show();

                } catch (RemoteException e) {

                    e.printStackTrace();

                    Toast.makeText(AIDLTestActivity.this, "赋值失败!", Toast.LENGTH_LONG).show();

                }

            }

        }

    };

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //本应用中需要在manifest中配置RemoteService

        bindService(new Intent("forServiceAidl"), conn, Service.BIND_AUTO_CREATE);

        setContentView(R.layout.activity_main);

    }

}

Android Studio DemoB

 

一、将DemoA中的aidl文件夹Copy过来,并放在main目录下(当然,自己想创建再写一次也可以)

 

二、在Activity中绑定开启服务

 

public class MainActivity extends AppCompatActivity {

    IPerson person;

    Button btn;

    private ServiceConnection conn = new ServiceConnection() {

        @Override

        public void onServiceDisconnected(ComponentName arg0) {

        }

        //因为有可能有多个应用同时进行RPC操作,所以同步该方法

        @Override

        public synchronized void onServiceConnected(ComponentName arg0, IBinder binder) {

            //获得IPerson接口

            person = IPerson.Stub.asInterface(binder);

        }

    };

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.btn);

        //该应用中不需要在manifest中配置RemoteService

        bindService(new Intent("forServiceAidl"), conn, Service.BIND_AUTO_CREATE);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View arg0) {

                if(person != null){

                    try {

                        //RPC方法调用

                        String name = person.getValue();

                        Toast.makeText(MainActivity.this, "远程进程调用成功!值为 : "+name, Toast.LENGTH_LONG).show();

                    } catch (RemoteException e) {

                        e.printStackTrace();

                        Toast.makeText(MainActivity.this, "远程进程调用失败! ", Toast.LENGTH_LONG).show();

                    }

                }

            }

        });

    }

}