知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
android怎樣用AIDLService傳遞復(fù)雜數(shù)據(jù)
發(fā)表時(shí)間:2020-11-13
發(fā)布人:葵宇科技
瀏覽次數(shù):33
大家都知道在Android中通過AIDL可以跨進(jìn)程調(diào)用Service中的數(shù)據(jù),網(wǎng)上也有很多實(shí)例,但是大部分實(shí)例都是關(guān)于基本數(shù)據(jù)類型的遠(yuǎn)程調(diào)用,很少講到復(fù)雜數(shù)據(jù)的調(diào)用,今天我用一個(gè)例子來演示一下怎樣用AIDL Service 傳遞復(fù)雜數(shù)據(jù)。
我們分2步開始:
第一步:部署我們的服務(wù)端,也就是Service端:
1:在Service端我先自定義2個(gè)類型:Person和Pet。因?yàn)槲覀冃枰邕M(jìn)程傳遞Person對象和Pet對象,所以Person類和Pet類都必須實(shí)現(xiàn)Parcelable接口,并要求在實(shí)現(xiàn)類中定義一個(gè)名為CREATER,類型為Parcelable.creator的靜態(tài)Field。
代碼如下:
[img]http://common.cnblogs.com/images/copycode.gif
1 package com.example.remoteservice; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 public class Person implements Parcelable { 7 int id; 8 String name; 9 String pass; 10 11 public Person() { 12 13 } 14 15 public Person(int id, String name, String pass) { 16 this.id = id; 17 this.name = name; 18 this.pass = pass; 19 } 20 21 @Override 22 public boolean equals(Object o) { 23 if (this == o) { 24 return true; 25 } 26 if (o == null) { 27 return false; 28 } 29 30 if (getClass() != o.getClass()) { 31 return false; 32 } 33 Person other = (Person) o; 34 35 if (name == null) { 36 if (other.name != null) { 37 return false; 38 } 39 } else if (!name.equals(other.name)) { 40 return false; 41 } 42 43 if (pass == null) { 44 if (other.pass != null) { 45 return false; 46 } 47 } else if (!pass.equals(other.pass)) { 48 return false; 49 } 50 51 return true; 52 } 53 54 @Override 55 public int hashCode() { 56 final int prime = 31; 57 int result = 1; 58 result = prime * result + (name == null ? 0 : name.hashCode()); 59 result = prime * result + (pass == null ? 0 : pass.hashCode()); 60 return result; 61 } 62 63 @Override 64 public int describeContents() { 65 66 return 0; 67 } 68 69 @Override 70 public void writeToParcel(Parcel arg0, int arg1) { 71 arg0.writeInt(id); 72 arg0.writeString(name); 73 arg0.writeString(pass); 74 } 75 76 public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { 77 78 @Override 79 public Person createFromParcel(Parcel source) { 80 81 return new Person(source.readInt(), source.readString(), source.readString()); 82 } 83 84 @Override 85 public Person[] newArray(int size) { 86 87 return new Person[size]; 88 } 89 }; 90 91 public int getId() { 92 return id; 93 } 94 95 public void setId(int id) { 96 this.id = id; 97 } 98 99 public String getName() { 100 return name; 101 } 102 103 public void setName(String name) { 104 this.name = name; 105 } 106 107 public String getPass() { 108 return pass; 109 } 110 111 public void setPass(String pass) { 112 this.pass = pass; 113 } 114 115 }
[img]http://common.cnblogs.com/images/copycode.gif
因?yàn)槲覀儠?huì)對Person進(jìn)行比較,所以在Person類中我重寫了
public int hashCode() 和 public boolean equals(Object o)方法
[img]http://common.cnblogs.com/images/copycode.gif
1 package com.example.remoteservice; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 public class Pet implements Parcelable { 7 String name; 8 float weight; 9 10 public Pet(String name, float weight) { 11 this.name = name; 12 this.weight = weight; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public float getWeight() { 24 return weight; 25 } 26 27 public void setWeight(float weight) { 28 this.weight = weight; 29 } 30 31 @Override 32 public int describeContents() { 33 34 return 1; 35 } 36 37 @Override 38 public void writeToParcel(Parcel dest, int flags) { 39 dest.writeString(name); 40 dest.writeFloat(weight); 41 42 } 43 44 public static final Parcelable.Creator<Pet> CREATOR = new Creator<Pet>() { 45 46 @Override 47 public Pet createFromParcel(Parcel source) { 48 49 return new Pet(source.readString(), source.readFloat()); 50 } 51 52 @Override 53 public Pet[] newArray(int size) { 54 55 return new Pet[size]; 56 } 57 }; 58 59 @Override 60 public String toString() { 61 62 return "name:" + this.name + ";weight:" + this.weight; 63 } 64 65 }
[img]http://common.cnblogs.com/images/copycode.gif
2:創(chuàng)建完自定義類型之后還需要用AIDL來定義它們,Person.aidl和Pet.aidl的代碼如下:
1 package com.example.remoteservice; 2 parcelable Person;
1 package com.example.remoteservice; 2 parcelable Pet;
3:完成1,2之后就可以使用AIDL定義通信接口了,在這里我定義一個(gè)IPet.aidl的接口,代碼如下:
[img]http://common.cnblogs.com/images/copycode.gif
1 package com.example.remoteservice; //必須導(dǎo)入包 2 import com.example.remoteservice.Person; //指定自定義類的位置 3 import com.example.remoteservice.Pet; 4 5 interface IPet 6 { 7 List<Pet> getPets(in Person owner);//這里的in表示Person對象是輸入的參數(shù) 8 }
[img]http://common.cnblogs.com/images/copycode.gif
4:服務(wù)端的最后一步就是實(shí)現(xiàn)Service了,當(dāng)然不要忘了注冊Service,代碼如下:
[img]http://common.cnblogs.com/images/copycode.gif
1 package com.example.remoteservice; 2 3 import com.example.remoteservice.IPet.Stub; 4 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import android.app.Service; 11 import android.content.Intent; 12 import android.os.IBinder; 13 import android.os.RemoteException; 14 import android.util.Log; 15 16 public class RemoteService extends Service { 17 18 private PetBinder petBinder; 19 20 private static Map<Person, List<Pet>> pets = new HashMap<Person, List<Pet>>(); 21 static { 22 ArrayList<Pet> list1 = new ArrayList<Pet>(); 23 list1.add(new Pet("candy", 2.2f)); 24 list1.add(new Pet("sandy", 4.2f)); 25 pets.put(new Person(1, "sun", "sun"), list1); 26 27 ArrayList<Pet> list2 = new ArrayList<Pet>(); 28 list2.add(new Pet("moon", 5.2f)); 29 list2.add(new Pet("hony", 6.2f)); 30 pets.put(new Person(1, "csx", "csx"), list2); 31 32 } 33 34 public class PetBinder extends Stub {// 繼承IPet接口中的Stub類,Stub類繼承了Binder類,所有PetBinder也間接的繼承了Binder類 35 36 @Override 37 public List<Pet> getPets(Person owner) throws RemoteException { 38 39 return pets.get(owner); 40 } 41 42 } 43 44 @Override 45 public IBinder onBind(Intent intent) { 46 47 Log.i("csx", "onBind"); 48 return petBinder; 49 } 50 51 @Override 52 public void onCreate() { 53 54 super.onCreate(); 55 Log.i("csx", "onCreate"); 56 petBinder = new PetBinder();// 實(shí)例化Binder 57 58 } 59 60 @Override 61 public boolean onUnbind(Intent intent) { 62 63 Log.i("csx", "onUnbind"); 64 return super.onUnbind(intent); 65 } 66 67 @Override 68 public void onDestroy() { 69 70 super.onDestroy(); 71 Log.i("csx", "onDestroy"); 72 } 73 74 }
[img]http://common.cnblogs.com/images/copycode.gif
這是我Service端的部署情況(其中MainActivity可以不用去實(shí)現(xiàn),因?yàn)槲覀冎惶峁┓?wù),沒有窗口顯示):
[img]http://images.cnitblog.com/blog/627366/201501/062209493438532.png
第二步:部署客戶端:
1.在客戶端新建一個(gè)包,命名需要和服務(wù)端放置aidl文件的包名相同(我這里是com.example.remoteservice),然后把服務(wù)端的Person.java,Pet.java,Person.aidl,Pet.aidl,IPet.aidl復(fù)制到這個(gè)包下面
[img]http://images.cnitblog.com/blog/627366/201501/062219327034112.png
2.在activity中綁定遠(yuǎn)程服務(wù)進(jìn)行數(shù)據(jù)交換,layout布局和activity代碼如下:
[img]http://common.cnblogs.com/images/copycode.gif
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.remoteclient.RemoteClient" > 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:orientation="vertical" > 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:orientation="horizontal" > 20 21 <EditText 22 android:id="@+id/editText_person" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_gravity="bottom" 26 android:ems="10" > 27 </EditText> 28 29 <Button 30 android:id="@+id/button_ok" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_gravity="bottom" 34 android:text="確定" /> 35 </LinearLayout> 36 37 <ListView 38 android:id="@+id/listView_pet" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" > 41 </ListView> 42 </LinearLayout> 43 44 </RelativeLayout>
[img]http://common.cnblogs.com/images/copycode.gif
[img]http://images.cnitblog.com/blog/627366/201501/062225239211380.png
[img]http://common.cnblogs.com/images/copycode.gif
1 package com.example.remoteclient; 2 3 import android.app.Service; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.RemoteException; 10 import android.support.v7.app.ActionBarActivity; 11 import android.util.Log; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.ArrayAdapter; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.ListView; 18 19 import com.example.remoteservice.IPet; 20 import com.example.remoteservice.Person; 21 import com.example.remoteservice.Pet; 22 23 import java.util.List; 24 25 public class RemoteClient extends ActionBarActivity { 26 27 public static final String REMOTE_SERVICE_ACTION = "com.example.remoteservice.RemoteService.ACTION"; 28 EditText editText; 29 Button button; 30 ListView listView; 31 32 IPet petService;// 聲明IPet接口 33 List<Pet> pets; 34 ServiceConnection conn = new ServiceConnection() { 35 36 @Override 37 public void onServiceDisconnected(ComponentName name) { 38 Log.i("csx", "onServiceDisconnected"); 39 conn = null; 40 } 41 42 @Override 43 public void onServiceConnected(ComponentName name, IBinder service) { 44 Log.i("csx", "onServiceConnected"); 45 petService = IPet.Stub.asInterface(service);// 通過遠(yuǎn)程服務(wù)的Binder實(shí)現(xiàn)接口 46 47 } 48 }; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.remote_client_layout); 54 editText = (EditText) findViewById(R.id.editText_person); 55 button = (Button) findViewById(R.id.button_ok); 56 listView = (ListView) findViewById(R.id.listView_pet); 57 58 Intent service = new Intent(); 59 service.setAction(REMOTE_SERVICE_ACTION); 60 61 bindService(service, conn, Service.BIND_AUTO_CREATE);// 綁定遠(yuǎn)程服務(wù) 62 63 button.setOnClickListener(new OnClickListener() { 64 65 @Override 66 public void onClick(View v) { 67 String personName = editText.getText().toString(); 68 if (personName == null || personName.equals("")) { 69 70 return; 71 } 72 73 try { 74 pets = petService.getPets(new Person(1, personName, personName));// 調(diào)用遠(yuǎn)程service的getPets方法 75 updataListView(); 76 77 } catch (RemoteException e) { 78 79 e.printStackTrace(); 80 } catch (NullPointerException e) { 81 e.printStackTrace(); 82 } 83 84 } 85 }); 86 87 } 88 89 public void updataListView() { 90 listView.setAdapter(null); 91 92 if (pets == null || pets.isEmpty()) { 93 return; 94 95 } 96 ArrayAdapter<Pet> adapter = new ArrayAdapter<Pet>(RemoteClient.this, 97 android.R.layout.simple_list_item_1, pets); 98 listView.setAdapter(adapter); 99 100 } 101 102 @Override 103 protected void onDestroy() { 104 105 unbindService(conn);// 解除綁定 106 super.onDestroy(); 107 } 108 109 }
[img]http://common.cnblogs.com/images/copycode.gif
到此為止所有的工作都完成了,下面我們看一下效果:我在編輯框中輸入“csx”,點(diǎn)擊確定,就會(huì)顯示出服務(wù)端RemoteService中pets的相應(yīng)數(shù)據(jù)。
[img]http://images.cnitblog.com/blog/627366/201501/062232365151887.jpg
其他精彩文章文章
jQuery教程(10)-DOM樹操作之內(nèi)容setter和getter方法
android學(xué)習(xí)筆記(37)使用 DatePickerDialog、TimePickerDialog
android學(xué)習(xí)筆記(36)使用AlertDialog創(chuàng)建自定義對話框
jQuery教程(1)-操作DOM之操作屬性
Spring mvc新手入門(11)-返回json 字符串的其他方式
更多關(guān)于android開發(fā)文章
相關(guān)案例查看更多
相關(guān)閱讀
- 開發(fā)框架
- 開發(fā)微信小程序
- 怎么做網(wǎng)站
- typescript
- 小程序表單
- 網(wǎng)站建設(shè)服務(wù)公司
- 報(bào)廢車回收
- 區(qū)塊鏈
- 前端開發(fā)
- 云南網(wǎng)站建設(shè)方案 doc
- 百度小程序
- 云南軟件設(shè)計(jì)
- 云南網(wǎng)站建設(shè)首選公司
- 網(wǎng)站制作哪家好
- 河南小程序制作
- 排名
- 網(wǎng)絡(luò)公司報(bào)價(jià)
- 企業(yè)網(wǎng)站
- 云南網(wǎng)站維護(hù)
- 云南網(wǎng)絡(luò)公司
- asp網(wǎng)站
- 網(wǎng)站建設(shè)快速優(yōu)化
- 生成海報(bào)
- 云南小程序定制
- 全國前十名小程序開發(fā)公司
- 英文網(wǎng)站建設(shè)公司
- 云南網(wǎng)站建設(shè)優(yōu)化
- 云南微信小程序開發(fā)
- 云南網(wǎng)站建設(shè)專家
- 云南軟件開發(fā)