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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

调用远程service aidl接口定义

發(fā)布時(shí)間:2024/4/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调用远程service aidl接口定义 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android studio 查看aidl定義的文件:當(dāng)你進(jìn)入你的AIDL文件并編寫好了之后,點(diǎn)擊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 //必須實(shí)現(xiàn)Parcelable接口 public class Student implements Parcelable {private int id;private String name;get set。。。。public int describeContents() { return 0;}//將當(dāng)前對(duì)象的屬性數(shù)據(jù)寫到Parcel包對(duì)象中(也就是打包) 打包解包在服務(wù)器端或client端都有可能,根據(jù)功能需求分類,如果服務(wù)器傳出數(shù)據(jù),則打包就在服務(wù)器端完成,如果客戶端傳輸數(shù)據(jù),則打包就在客戶端完成public void writeToParcel(Parcel dest, int flags) {dest.writeInt(this.id);dest.writeString(this.name);}// 添加一個(gè)靜態(tài)成員,名為CREATOR,該對(duì)象實(shí)現(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自動(dòng)生成一個(gè)通信接口類
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接口定义的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。