Android之AIDL入门示例
做了一个小例子:
TestAIDLServer.apk是AIDL文件的服务端 TestAIDLProxy.apk是客户端
代码如下
TestAIDLServer中
package com.test.aidl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class MyService extends Service {public class MyServiceImpl extends IMyService.Stub {public String getValue() throws RemoteException {return "Android 链接成功";}}public IBinder onBind(Intent intent) {return new MyServiceImpl();}}IMyService.aidl中package com.test.aidl;interface IMyService{ String getValue();}AndroidManifest.xml中<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.test.aidl" android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".TestAIDLServer" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".MyService"><intent-filter><action android:name="com.test.aidl.IMyService" /></intent-filter></service></application></manifest>
TestAIDLProxy.apk中
吧TestAIDLServer中自动生成的IMyService.java连同包名一起考过来!
TestAIDLProxy.javapackage com.test.proxy;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.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.test.aidl.IMyService;public class TestAIDLProxy extends Activity implements OnClickListener {private IMyService myService = null;private Button mButton1;private Button mButton2;private TextView textView;private ServiceConnection serviceConnection = new ServiceConnection() {public void onServiceConnected(ComponentName name, IBinder service) {// 获得服务对象myService = IMyService.Stub.asInterface(service);mButton2.setEnabled(true);}public void onServiceDisconnected(ComponentName name) {}};public void onClick(View view) {switch (view.getId()) {case R.id.button01:textView.setText(":-)");// 绑定AIDL服务bindService(new Intent("com.test.aidl.IMyService"),serviceConnection, Context.BIND_AUTO_CREATE);break;case R.id.button02:try {textView.setText(myService.getValue()); // 调用服务端的getValue方法} catch (Exception e) {}break;}}public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mButton1 = (Button) findViewById(R.id.button01);mButton1.setOnClickListener(this);mButton2 = (Button) findViewById(R.id.button02);mButton2.setOnClickListener(this);mButton2.setEnabled(false);textView = (TextView) findViewById(R.id.mTextView01);}}<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><Button android:text="@string/buttonstr1" android:id="@+id/button01"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="@string/buttonstr2" android:id="@+id/button02"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><TextView android:layout_width="fill_parent" android:id="@+id/mTextView01"android:layout_height="wrap_content" android:text="@string/textviewstr" /></LinearLayout>