AIDL使用解析

xiaoxiao2021-02-28  110

1.AIDL

AIDL(Android Interface Define Language) 是IPC进程间通信方式的一种.用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码.

2.创建.aidl文件

点击构建按钮自动生成接口文件:

3.创建service文件

创建内部类并继承接口类的stub子类

4.在ui中引用

Context.bindservice(Intent,ServiceConnection,Context.BIND_AUTO_CREATE);

需要3个参数  前两个新建就可以了。

serviceconnection中需要实现2个方法,如下图:

在方法中将service中提供的ibinder转换为客户端接口。

5.引用

在需要返回服务端处理结果的地方引用接口类中的方法。

6.详细代码

// IMyAidlInterface.aidl package com.example.lanxumit.myapplication; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * 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); int add(int a,int b); }

package com.example.lanxumit.myapplication; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyService extends Service { private MyIbinder myIbinder = new MyIbinder(); public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return myIbinder; } private class MyIbinder extends IMyAidlInterface.Stub{ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public int add(int a, int b) throws RemoteException { return a+b; } } } package com.example.lanxumit.myapplication; import android.content.ComponentName; import android.content.Context; 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.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView add; IMyAidlInterface iMyAidlInterface; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { iMyAidlInterface = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); add = (TextView) findViewById(R.id.add); Intent intent = new Intent(this,MyService.class); this.bindService(intent,conn, Context.BIND_AUTO_CREATE); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { add.setText(String.valueOf(iMyAidlInterface.add(1,1))); } catch (RemoteException e) { e.printStackTrace(); } } }); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); unbindService(conn); } }
转载请注明原文地址: https://www.6miu.com/read-34572.html

最新回复(0)