了解Android的Aidl机制

来源:互联网 发布:淘宝网品牌男装 编辑:程序博客网 时间:2024/05/16 01:57

想想工作都小半年了,对于这种机制还是似懂非懂,一直没用到过,也就一直不懂。

今天抽空研究一下,万事开头难,难者不会,会者不难!


算是一个比较难理解的概念了,Android的所谓AIDL 机制,一涉及都AIDL各种博客就开始说,需要懂Android中序列化,Android中Service,Binder,Messager等诸多理论,想想自己这些理论都没怎么掌握,看到这里就知难而退了,能不能少点套路,多点实际demo,一下子就能学会基本用法。因为对于不了解AIDL机制的同学们,你扯一大堆没用的,我们这种水平看完还是没用,直到我跑了一个demo,记录下全过程。


AIDL机制,说太多理论,不进行实际的操作还是不明白。进程间通信,那就让activity与Service之间通信试一试!

这里可能有些同学会有疑问,activity与service同样属于android4大组件之一,一般情况下一个Application只有一个进程,在一个应用内怎么可能涉及到远程通信呢?service 包括本地服务和远程服务,区别就在于是否属于同一个进程,通过属性值    android:process=“:remote” ,在AndroidManifest.xml文件中配置这一句话,就表示2者分属不同进程,是不是很简单。

<service    android:name=".MyService"    android:process=":remote"/>

MainActivity.java


package com.example.mytry;import android.app.Activity;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.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends Activity implements View.OnClickListener {    private Button button, button2;    MyAidl aidlInterface;    private ServiceConnection serviceConnection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            Log.i("liuting", "onServiceConnected: " + name.getClassName());            aidlInterface = MyAidl.Stub.asInterface(service);        }        @Override        public void onServiceDisconnected(ComponentName name) {            Log.i("liuting", "onServiceDisconnected: " + name.getClassName());            aidlInterface = null;        }    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        button2 = (Button) findViewById(R.id.button2);        button.setOnClickListener(this);        button2.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if (v.getId() == R.id.button) {            bind();        } else if (v.getId() == R.id.button2) {            startMethod();        }    }    /**     * 实现aidl中的方法     */    private void startMethod() {        try {            aidlInterface.basicTypes("我是信息dd:原来是我把信息传给服务的");        } catch (RemoteException e) {            e.printStackTrace();        }    }    /**     * 绑定到服务上去     */    private void bind() {        Intent intent = new Intent(MainActivity.this, MyService.class);        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);    }}

MyService.java

package com.example.mytry;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import static android.content.ContentValues.TAG;public class MyService extends Service {    public MyService() {    }    private final MyAidl.Stub mBinder =new MyAidl.Stub() {        @Override        public void basicTypes(String dd) throws RemoteException {            Log.i("liuting_dd", "收到来自activity的信息: "+dd);        }    };    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        return mBinder;    }}

MyAidl.aidl


// MyAidl.aidlpackage com.example.mytry;// Declare any non-default types here with import statementsinterface MyAidl {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */    void basicTypes(String dd);}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="bindService" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="startMethod" /></LinearLayout>


只要以上这几个文件就搞定了。5个文件demo就跑起来了。

aidl文件的创建不知道的看这里,知道的可以忽略。android studio中new 可以new出android中任何格式的文件。



就这样一个工具,之前看到的时候让我欣喜了好几天,android studio就是超级强大!
demo跑起来了,是否有些不理解!

——AIDL:Android Interface Definition Language,即Android接口定义语言。
详情见https://developer.android.com/guide/components/aidl.html?hl=zh-cn





原创粉丝点击