调用远程service aidl接口定义
生活随笔
收集整理的這篇文章主要介紹了
调用远程service aidl接口定义
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android studio 查看aidl定義的文件:當(dāng)你進入你的AIDL文件并編寫好了之后,點擊AS上方菜單欄中的Build->Make Project,之后便可以在當(dāng)前工程的app/build/generated/source/aidl/debug中找到系統(tǒng)為我們生成的.java文件了。
?
Service端
<service android:name="com.atguigu.l07_service.remote.MyRemoteService"><intent-filter><action android:name="com.atguigu.l07_service.remote.MyRemoteService.Action"/></intent-filter></service> public class MyRemoteService extends Service {@Overridepublic IBinder onBind(Intent intent) {Log.e("TAG", "onBind()");return new StudentService();}@Overridepublic boolean onUnbind(Intent intent) {Log.e("TAG", "onUnbind()");return super.onUnbind(intent);}//處理Student相關(guān)的業(yè)務(wù)邏輯類class StudentService extends IStudentService.Stub {@Overridepublic Student getStudentById(int id) throws RemoteException {Log.e("TAG", "Service getStudentById() "+id);return new Student(id, "Tom", 10000);}}}《------------------------------start定義aidl接口--------------------------------------------》
定義自定義類型Student //必須實現(xiàn)Parcelable接口 public class Student implements Parcelable {private int id;private String name;get set。。。。public int describeContents() { return 0;}//將當(dāng)前對象的屬性數(shù)據(jù)寫到Parcel包對象中(也就是打包) 打包解包在服務(wù)器端或client端都有可能,根據(jù)功能需求分類,如果服務(wù)器傳出數(shù)據(jù),則打包就在服務(wù)器端完成,如果客戶端傳輸數(shù)據(jù),則打包就在客戶端完成public void writeToParcel(Parcel dest, int flags) {dest.writeInt(this.id);dest.writeString(this.name);}// 添加一個靜態(tài)成員,名為CREATOR,該對象實現(xiàn)了Parcelable.Creator接口public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {public Student createFromParcel(Parcel source) {return new Student(source.readInt(), source.readString());}public Student[] newArray(int size) {return new Student[size];}}; }創(chuàng)建文件:Student.aidl package com.atguigu.service.test.remote; parcelable Student; 創(chuàng)建文件:IStudentService.aidl package com.atguigu.service.test.remote; import com.atguigu.service.test.remote.Student;interface IStudentService {Student getStudentById(int id); } eclipse自動生成一個通信接口類
package com.atguigu.service.test.remote; public interface IStudentService extends android.os.IInterface{....... }
?
?
《------------------------------end定義aidl接口--------------------------------------------》
client
?
public class MainActivity extends Activity {private EditText et_aidl_id;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_aidl_id = (EditText) findViewById(R.id.et_aidl_id);}private ServiceConnection conn;private IStudentService studentService;public void bindRemoteService(View v) {Intent intent = new Intent("com.atguigu.l07_service.remote.MyRemoteService.Action");if (conn == null) {conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name,IBinder service) {Log.e("TAG", "onServiceConnected()");studentService = IStudentService.Stub.asInterface(service);}};bindService(intent, conn, Context.BIND_AUTO_CREATE);Toast.makeText(this, "綁定Service", 0).show();} else {Toast.makeText(this, "已經(jīng)綁定Service", 0).show();}}public void invokeRemote(View v) throws RemoteException {if(studentService!=null) {int id = Integer.parseInt(et_aidl_id.getText().toString());Student student = studentService.getStudentById(id);Toast.makeText(this, student.toString(), 0).show();}}public void unbindRemoteService(View v) {if (conn != null) {unbindService(conn);conn = null;studentService = null;Toast.makeText(this, "解綁Service", 0).show();} else {Toast.makeText(this, "還未綁定Service", 0).show();}} }?
轉(zhuǎn)載于:https://www.cnblogs.com/znsongshu/p/9355893.html
總結(jié)
以上是生活随笔為你收集整理的调用远程service aidl接口定义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AntDB上使用表空间
- 下一篇: OkHttp如何移除User-Agent