使用AIDL远程调用服务中的方法

AIDL:android interface define language(接口定义语言)

作用:方便远程调用其他服务中的方法

注意:安卓四大组件都要在清单文件注册

aidl创建图:

AIDL的全称是什么?如何工作?能处理哪些类型的数据?
AIDL全称Android Interface Definition Language(AndRoid接口描述语言)是一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程跨界对象访问的目的.AIDL的IPC的机制和COM或CORBA类似, 是基于接口的,但它是轻量级的。它使用代理类在客户端和实现层间传递值. 

如果要使用AIDL, 需要完成2件事情: 

1. 引入AIDL的相关类.; 

2. 调用aidl产生的class.

理论上, 参数可以传递基本数据类型和String, 还有就是Bundle的派生类, 不过在Eclipse中,目前的ADT不支持Bundle做为参数

具体实现步骤如下:
1、创建AIDL文件, 在这个文件里面定义接口, 该接口定义了可供客户端访问的方法和属性。
2、编译AIDL文件, 用Ant的话, 可能需要手动, 使用Eclipse plugin的话,可以根据adil文件自动生产java文件并编译, 不需要人为介入.
3、在Java文件中, 实现AIDL中定义的接口. 编译器会根据AIDL接口, 产生一个JAVA接口。这个接口有一个名为Stub的内部抽象类,它继承扩展了接口并实现了远程调用需要的几个方法。接下来就需要自己去实现自定义的几个接口了.
4、向客户端提供接口ITaskBinder, 如果写的是service,扩展该Service并重载onBind ()方法来返回一个实现上述接口的类的实例。
5、在服务器端回调客户端的函数. 前提是当客户端获取的IBinder接口的时候,要去注册回调函数, 只有这样, 服务器端才知道该调用那些函数
AIDL语法很简单,可以用来声明一个带一个或多个方法的接口,也可以传递参数和返回值。 由于远程调用的需要, 这些参数和返回值并不是任何类型.

下面是些AIDL支持的数据类型:
1. 不需要import声明的简单Java编程语言类型(int,boolean等)
2. String, CharSequence不需要特殊声明
3. List, Map和Parcelables类型, 这些类型内所包含的数据成员也只能是简单数据类型, String等其他比支持的类型.
(另外: 我没尝试Parcelables, 在Eclipse+ADT下编译不过, 或许以后会有所支持).

实现接口时有几个原则:
.抛出的异常不要返回给调用者. 跨进程抛异常处理是不可取的.
.IPC调用是同步的。如果你知道一个IPC服务需要超过几毫秒的时间才能完成地话,你应该避免在Activity的主线程中调用。也就是IPC调用会挂起应用程序导致界面失去响应. 这种情况应该考虑单起一个线程来处理.
.不能在AIDL接口中声明静态属性。

IPC的调用步骤:
1. 声明一个接口类型的变量,该接口类型在.aidl文件中定义。
2. 实现ServiceConnection。
3. 调用ApplicationContext.bindService(),并在ServiceConnection实现中进行传递.
4. 在ServiceConnection.onServiceConnected()实现中,你会接收一个IBinder实例(被调用的Service). 调用
YourInterfaceName.Stub.asInterface((IBinder)service)将参数转换为YourInterface类型。
5. 调用接口中定义的方法。你总要检测到DeadObjectException异常,该异常在连接断开时被抛出。它只会被远程方法抛出。
6. 断开连接,调用接口实例中的ApplicationContext.unbindService()
参考:http://buaadallas.blog.51cto.com/399160/372090

interface InterfaceAidl {
   void interfacePay();
}
public class MyService extends Service {

    class MyBinder extends InterfaceAidl.Stub {
        @Override
        public void interfacePay() throws RemoteException {
            pay();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        MyBinder binder = new MyBinder();
        return binder;
    }

    private void pay() {
        Log.i("service","支付成功");
    }
}
public class MainActivity extends AppCompatActivity {
    private Intent intent;
    private MyServiceConnection myServiceConnection;
    private InterfaceAidl interfaceAidl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(this,MyService.class);
        myServiceConnection = new MyServiceConnection();
    }


    public void bind(View view){
        bindService(intent, myServiceConnection, BIND_AUTO_CREATE);
    }

    public void pay(View view){
        try {
            interfaceAidl.interfacePay();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    class MyServiceConnection implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            interfaceAidl = InterfaceAidl.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

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

远程调用服务

远程服务。
  1:将服务所在的项目中man目录下的aidl目录复制粘贴到当前项目。然后同步。会在项目的build-generated--source--
   aidl---debug--生成一个PayInterface.java文件.
  2:在MainActivity里创建意图。
    intent = new Intent();
          //从5.0开始,intent不能使用隐式意图
          intent.setAction("aidl_server.pay");
          intent.setPackage("com.qf.gao.d25_aidl_service");//指定包名,
  3:其他的跟调用本地服务一样。

interface InterfaceAidl {
   void interfacePay();
}
<service android:name=".MyService">
            <intent-filter>
                <action android:name="aidl_interface.pay"></action>
            </intent-filter>
        </service>
public class MainActivity extends AppCompatActivity {

    private Intent intent;
    private InterfaceAidl interfaceAidl;
    private MyServiceConnection myServiceConnection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent();
        myServiceConnection = new MyServiceConnection();
        intent.setAction("aidl_interface.pay");
        intent.setPackage("com.example.administrator.aidlservice");
    }

    public void bind(View view){
        bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
    }

    public void pay(View view){
        try {
            interfaceAidl.interfacePay();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            interfaceAidl  = InterfaceAidl.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

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

 

posted @ 2016-04-18 11:01  嘉禾世兴  阅读(2892)  评论(0编辑  收藏  举报