安卓aidl的详细讲解(终极讲解)

来源:互联网 发布:蒙特卡洛算法量化 编辑:程序博客网 时间:2024/06/08 02:51

1,aidl以前只是知道这个东西,就是没用过,今天到用的时候了,终于把他搞定,开始:

先上服务端的工程结构图:


客户端的工程结构图:



大家看到我两个工程都有相同的aidl文件,且包名相同;


2,上aidl的代码,很简单的代码,一个返回字符串的抽象方法:

package com.myzxing;// Declare any non-default types here with import statementsinterface MyAidlInterface {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */     String order();}

3,上服务端的服务的代码,重要的是在onBind方法里,对aidlInterface的对象返回,这样这个服务就和aidl关联起来了。


public class MyAidlService extends Service {    public MyAidlService() {    }    @Override    public void onRebind(Intent intent) {        super.onRebind(intent);        System.out.println("--- :onRebind");    }    @Override    public void onCreate() {        super.onCreate();        System.out.println("--- :onCreate");    }    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        System.out.println("--- :start serve");    }    @Override    public IBinder onBind(Intent intent) {        System.out.println("--- :IBinder serve");        return aidlInterface;    }    private final MyAidlInterface.Stub aidlInterface=new MyAidlInterface.Stub() {        @Override        public String order() throws RemoteException {            return "开启wifi";        }    };}
服务的清单注册:
<service    android:name="com.service.MyAidlService"    android:enabled="true"    android:exported="true"    android:process=":remote">    <intent-filter>        <action android:name="com.myzxing.MyAidlInterface"/>        <category android:name="android.intent.category.DEFAULT"/>    </intent-filter></service>


4,上客户端的activity的代码:

public class MainActivity extends AppCompatActivity {TextView textView; //显示从服务端获取的指令    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView= ((TextView) findViewById(R.id.textview));    }    //绑定远程服务    public void click(View view){        Bundle args = new Bundle();        Intent intent = new Intent("com.myzxing.MyAidlInterface");//com.myzxing.MyAidlInterface是服务端的服务的action(也是aidl的完整类名)        intent.putExtras(args);        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);    }    //获取远程服务的指令    public void click1(View view) throws RemoteException {        String order = mService.order();        textView.setText(order);    }    MyAidlInterface mService;    private ServiceConnection mConnection = new ServiceConnection(){        public void onServiceConnected(ComponentName className,                                       IBinder service){           // Log("connect service");            mService = MyAidlInterface.Stub.asInterface(service);        }        public void onServiceDisconnected(ComponentName className){            mService = null;        }    };}

5,大家看到我服务端的服务的,这个代码,就是给客户端的信息;测试结果如下:

private final MyAidlInterface.Stub aidlInterface=new MyAidlInterface.Stub() {        @Override        public String order() throws RemoteException {            return "开启wifi";        }    };

点击绑定服务,然后点击得到结果,展示如下:

点击绑定服务,然后点击得到结果,展示如下:



6,到此,aidl'已经详细讲解完毕,希望对大家有用》》》》》》


0 0