第三步:写一个与Book类对应的aidl,命名为Book.aidl,代码非常简单,代码如下:
- parcelable Book;
第四步:新建一个名为AidlServerService的Service.代码如下:
- package com.chapter8.aidl;
- import com.chapter8.aidl.IAIDLServerService.Stub;
- import com.chapter8.aidl.IAIDLServerService;
- import Android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.RemoteException;
- public class AidlServerService extends Service {
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- /**
- * 在AIDL文件中定义的接口实现。
- */
- private IAIDLServerService.Stub mBinder = new Stub() {
- public String sayHello() throws RemoteException {
- return "Hello";
- }
- public Book getBook() throws RemoteException {
- Book mBook = new Book();
- mBook.setBookName("Android应用开发");
- mBook.setBookPrice(50);
- return mBook;
- }
- };
- }
第五步:在AndroidManifest.xml注册Service,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.chapter8.aidl"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name="AidlServerActivity"
- 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="AidlServerService"
- android:process=":remote">
- <intent-filter>
- <action android:name="com.chapter8.aidl.IAIDLServerService"></action>
- </intent-filter>
- </service>
- </application>
- </manifest>
客户端的具体实现步骤:
第一步:新建客户端工程,目录结构如下:
