日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

调用远程service aidl接口定义

發布時間:2024/4/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调用远程service aidl接口定义 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android studio 查看aidl定義的文件:當你進入你的AIDL文件并編寫好了之后,點擊AS上方菜單欄中的Build->Make Project,之后便可以在當前工程的app/build/generated/source/aidl/debug中找到系統為我們生成的.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相關的業務邏輯類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 //必須實現Parcelable接口 public class Student implements Parcelable {private int id;private String name;get set。。。。public int describeContents() { return 0;}//將當前對象的屬性數據寫到Parcel包對象中(也就是打包) 打包解包在服務器端或client端都有可能,根據功能需求分類,如果服務器傳出數據,則打包就在服務器端完成,如果客戶端傳輸數據,則打包就在客戶端完成public void writeToParcel(Parcel dest, int flags) {dest.writeInt(this.id);dest.writeString(this.name);}// 添加一個靜態成員,名為CREATOR,該對象實現了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];}}; }
創建文件:Student.aidl package com.atguigu.service.test.remote; parcelable Student; 創建文件: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, "已經綁定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();}} }

?

轉載于:https://www.cnblogs.com/znsongshu/p/9355893.html

總結

以上是生活随笔為你收集整理的调用远程service aidl接口定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。