一.目录结构:
二.aidl类:
// BlueToothAidlInterface.aidl package com.example.lxb.bluetoothaidl; /** * Declare any non-default types here with import statements * 此处需要注意: * 1.引入接口返回对象所在包。 * 2.对象所在包名应该与当前这个接口名包一致 */ import com.example.lxb.bluetoothaidl.HelloMsg; interface BlueToothAidlInterface { //测试接口 HelloMsg sayHello(); //in标志表示客户端向其他进程写信息 void writeToOtherProcess(in HelloMsg helloMsg); //out标志表示客户端通过aidl读取其他进程传过来的信息 HelloMsg readFromOtherProcess(); }
// HelloMsg.aidl package com.example.lxb.bluetoothaidl; import com.example.lxb.bluetoothaidl.HelloMsg; /** *声明数据实体 */ parcelable HelloMsg;
三。服务类:
package com.example.lxb.bluetoothaidl.server; import android.app.IntentService; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.support.annotation.Nullable; import com.example.lxb.bluetoothaidl.BlueToothAidlInterface; import com.example.lxb.bluetoothaidl.HelloMsg; /** * * IntentService有以下特点: * (1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。 * * (2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。 * * (3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。 * * (4) 默认实现的onBind()返回null * * (5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中 * Created by lxb on 2017/2/14. */ public class BTAidlIntentSerice extends IntentService { private HelloMsg helloMsg; @Override public void onCreate() { super.onCreate(); helloMsg = new HelloMsg(); helloMsg.setMsg("BTAidlIntentSerice"); helloMsg.setPid(1989); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } /** * 处理外部传进来的队列 * @param intent */ @Override protected void onHandleIntent(Intent intent) { } @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } private BlueToothAidlInterface.Stub mBinder = new BlueToothAidlInterface.Stub(){ @Override public HelloMsg sayHello() throws RemoteException { return helloMsg; } @Override public void writeToOtherProcess(HelloMsg helloMsg) throws RemoteException { BTAidlIntentSerice.this.helloMsg = helloMsg; } @Override public HelloMsg readFromOtherProcess() throws RemoteException { return BTAidlIntentSerice.this.helloMsg; } }; /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public BTAidlIntentSerice(String name) { super(name); } public BTAidlIntentSerice(){ super("BTAidlIntentSerice"); } }
四。mainfest文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lxb.bluetoothaidl"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".server.BTAidlService" android:exported="true"> <intent-filter> <action android:name="com.liu.aidl"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> <service android:name=".server.BTAidlIntentSerice" android:exported="true"> <intent-filter> <action android:name="com.liu.aidl"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application> </manifest>
五。使用类
package com.example.lxb.bluetoothaidl; import android.app.Activity; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.lxb.bluetoothaidl.server.BTAidlIntentSerice; import com.example.lxb.bluetoothaidl.server.BTAidlService; public class MainActivity extends Activity { private Button mAidl; private Button mAidlWrite; private Button mAidlRead; private boolean mBind = false; private BlueToothAidlInterface mBTRemoteSerice = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAidl = (Button) findViewById(R.id.btn_aidl); mAidlWrite = (Button)findViewById(R.id.btn_write); mAidlRead = (Button)findViewById(R.id.btn_read); EventClickListener clickListener = new EventClickListener(); mAidl.setOnClickListener(clickListener); mAidlWrite.setOnClickListener(clickListener); mAidlRead.setOnClickListener(clickListener); } private class EventClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_aidl: if (mBind) { try { System.out.println("36------------------" + mBTRemoteSerice.sayHello().getMsg()); } catch (RemoteException e) { e.printStackTrace(); } } break; case R.id.btn_write: HelloMsg helloMsg = new HelloMsg(); helloMsg.setMsg("write info test"); helloMsg.setPid(2018); if(mBind){ try { mBTRemoteSerice.writeToOtherProcess(helloMsg); } catch (RemoteException e) { e.printStackTrace(); } } break; case R.id.btn_read: if(mBind){ try{ System.out.println("74------------------" + mBTRemoteSerice.readFromOtherProcess().getMsg().toString()); }catch(RemoteException e){ e.printStackTrace(); } } break; } } } @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this, BTAidlIntentSerice.class); intent.setAction("com.liu.aidl"); intent.setPackage("com.example.lxb.bluetoothaidl"); bindService(intent, mConnection, BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); unbindService(mConnection); mBind = false; } /** * 服务连接器 */ private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mBTRemoteSerice = BlueToothAidlInterface.Stub.asInterface(service); mBind = true; } @Override public void onServiceDisconnected(ComponentName name) { mBTRemoteSerice = null; mBind = false; } }; }
六。数据对象:
package com.example.lxb.bluetoothaidl; import android.os.Parcel; import android.os.Parcelable; /** * 测试信息实体 * Created by lxb on 2017/2/14. */ public class HelloMsg implements Parcelable { private String msg; private int pid; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.msg); dest.writeInt(this.pid); } public HelloMsg() { } protected HelloMsg(Parcel in) { this.msg = in.readString(); this.pid = in.readInt(); } /** * 参数是一个Parcel,用它来存储与传输数据 * @param dest */ public void readFromParcel(Parcel dest) { //注意,此处的读值顺序应当是和writeToParcel()方法中一致的 msg = dest.readString(); pid = dest.readInt(); } public static final Parcelable.Creator<HelloMsg> CREATOR = new Parcelable.Creator<HelloMsg>() { @Override public HelloMsg createFromParcel(Parcel source) { return new HelloMsg(source); } @Override public HelloMsg[] newArray(int size) { return new HelloMsg[size]; } }; }
七。布局类:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.lxb.bluetoothaidl.MainActivity"> <Button android:id="@+id/btn_aidl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通读取服务信息" /> <Button android:id="@+id/btn_write" android:layout_below="@+id/btn_aidl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="客户端写信息到服务,并通过aidl传递给其他应用" /> <Button android:id="@+id/btn_read" android:layout_below="@+id/btn_write" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从服务中读取其他应用通过adil写进来的信息" /> </RelativeLayout>