(AIDL)Android Interface definition language
2017-01-03 11:16
393 查看
AIDL:接口定义语言(Android Interface definition language)
aidl是实现进程间通信的一种方法,习惯上用
服务端 和 客户端 来区分通信的两端
1、服务端:被调用的一端
2、客户端:调用的一端
服务端:
1.1、创建.aidl文件,如下图:
客户端与服务端创建的.aidl文件应完全相同,包括包名,.aidl的文件名,以及.aidl文件中定义的接口,如下图:
public classIRemoteService extends Service
{
客户端:
aidl是实现进程间通信的一种方法,习惯上用
服务端 和 客户端 来区分通信的两端
1、服务端:被调用的一端
步骤:
1、创建.aidl文件
2、实现接口
3、将接口暴露给客户端
2、客户端:调用的一端
步骤:
1、绑定服务
2、绑定服务后回调被绑定服务中的方法
下面是一个使用aidl进行加法运算的Demo
服务端:
1.1、创建.aidl文件,如下图:
客户端与服务端创建的.aidl文件应完全相同,包括包名,.aidl的文件名,以及.aidl文件中定义的接口,如下图:
IMyAidlInterface.aidl:
interface IMyAidlInterface {
/** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ int add(int num1,int num2); }
在服务端中创建 IRemoteService ,如上图,并将IRemoteService注册到清单文件中:
创建 IRemoteService:
public classIRemoteService extends Service
{
//创建.aidl文件---(将IRemoteService注册到清单文件中)--->实现接口------>将接口暴露给客户端 //1.2实现接口 private IBinder mIBinder = new IMyAidlInterface.Stub() { @Override public int add(int num1, int num2) throws RemoteException { return num1 + num2; } }; //1.3将接口暴露给客户端 @Nullable @Override public IBinder onBind(Intent intent) { return mIBinder; } }
注册IRemoteService:
<service android:name=".IRemoteService" android:exported="true" android:process=":remote"/>
客户端:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" tools:context="com.viciy.aidlclient.MainActivity"> <EditText android:id="@+id/et_num1" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textColor="@android:color/black" android:textSize="30dp"/> <EditText android:id="@+id/et_num2" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:textColor="@android:color/black" android:textSize="30dp"/> <EditText android:id="@+id/et_num3" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center"/> <Button android:id="@+id/bt_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add"/>
</LinearLayout>
MainActivity.class:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView num1; private TextView num2; private TextView num3; private Button btAdd; private int num4; IMyAidlInterface myAidlInterface; //2.2服务绑定后的回调 private ServiceConnection conn = new ServiceConnection() {
/** * 服务绑定成功后调用,获取服务端的接口; * 这里的service就是服务端onBind返回的, * 已实现的IBinder接口 */ @Override public void onServiceConnected(ComponentName name, IBinder service) { myAidlInterface = IMyAidlInterface.Stub.asInterface(service); }
/** * 解除绑定时调用; * 清空接口,防止内存溢出 */ @Override public void onServiceDisconnected(ComponentName name) { myAidlInterface = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.1绑定服务 bindService(); //初始化界面 initView(); } //绑定服务 private void bindService() { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.viciy.aidlserver", "com.viciy.aidlserver.IRemoteService")); bindService(intent,conn, Context.BIND_AUTO_CREATE); } private void initView() { num1=(TextView)findViewById(R.id.et_num1); num2=(TextView)findViewById(R.id.et_num2); num3=(TextView)findViewById(R.id.et_num3); btAdd=(Button)findViewById(R.id.bt_add); btAdd.setOnClickListener(this); } @Override public void onClick(View v) { int num1 = Integer.parseInt(this.num1.getText().toString()); int num2 = Integer.parseInt(this.num2.getText().toString()); try { num4 = myAidlInterface.add(num1,num2); } catch (RemoteException e) { e.printStackTrace(); } num3.setText(num4+""); } @Override protected void onDestroy() { super.onDestroy(); //解除绑定 unbindService(conn); } }
相关文章推荐
- AIDL(Android interface Definition Language)
- Android AIDL(Android Interface Definition Language)介绍
- Android Interface Definition Language (AIDL)
- Android Interface Definition Language (AIDL)
- Android Interface Definition Language (AIDL)
- Android翻译:Android interface definition language(aidl)
- Android Interface Definition Language (AIDL)——翻译自developer.android.com
- Android Interface Definition Language (AIDL)
- Android AIDL(Android Interface Definition Language)介绍
- Android Interface Definition Language (AIDL)详解
- Android Interface Definition Language (AIDL)
- Android Interface Definition Language (AIDL)
- IPC——AIDL(Android Interface Definition Language)那些事儿
- Android Interface Definition Language (AIDL) android接口定义语言 开发文档翻译 - 1
- Android AIDL(Android Interface Definition Language)介绍
- Android AIDL(Android Interface Definition Language)介绍
- Android Interface Definition Language (AIDL) 接口描述语言
- Android Interface Definition Language (AIDL)实现应用间通信
- Android Interface Definition Language (AIDL)详解
- Android Interface Definition Language (AIDL)