跨进程访问(AIDL服务)—Service(三)

来源:互联网 发布:淘宝如何打造爆款 编辑:程序博客网 时间:2024/05/17 03:29

       最近这几天一直都在写一个小软件,所以一直未更新博客,哎,真是计划赶不上变化。因此这一期拖到现在来更新,更新博客,我觉得还是有助于对技术的深刻理解,可能当时你当时并不是很懂,但是通过写写博客,可能会有所领悟,还有助于问题错误的分析,或者是经过了许久,这个技术忘记了,但是博客的好处就是帮助我温习一遍,然后又有了新的体会,总之一句话,多写写博客,生活更有滋味啊。


上一期讲了如何用AIDL服务传递一些简单的数据,这一期将要复习如何传递一些更加复杂的数据。

为了弄清楚这个问题,首先我们需要了解什么样的类型的数据呢,就像一个自定义的Notification一样只支持有限的控件。

而AIDL服务支持的的数据主要分两种,一种是需要导入import的数据,比如AIDL自动生成的接口,实现android.os.Parcelalbe接口的类。另一种是简单类型,比如int,char,boolean,这些简单类型用上节课给出的方法就可以解决。第一种比较复杂一些,我在测试的时候碰到了很多问题,接下来我会一一来说。

实现android.os.Parcelable,除了要建立这个接口类,还要单独建立一个aidl文件,并用Parcelable 关键字来定义。


建立一个IMyService.aidl文件。

package mobile.android.complex.type.aidl;import mobile.android.complex.type.aidl.Product;interface IMyService  {      Map getMap(in String country, in Product product);    Product getProduct();     }       

Product 这个类实现了Parcelable 这个接口,需要导入这个类。如果方法的类型是非简单型,例如String list或是自定义的类,需要用in  out 或者inout 进行修饰,in 表示可以被客户端设置,out表示可以被服务端设置,inout两者都可以设置。

下面是Product类

package mobile.android.complex.type.aidl;import android.os.Parcel;import android.os.Parcelable;public class Product implements Parcelable{private int id;private String name;private float price;public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>(){public Product createFromParcel(Parcel in){return new Product(in);}public Product[] newArray(int size){return new Product[size]; }};    public Product()    {        }private Product(Parcel in){readFromParcel(in);}@Overridepublic int describeContents(){// TODO Auto-generated method stubreturn 0;}public void readFromParcel(Parcel in){id = in.readInt();name = in.readString();price = in.readFloat();}@Overridepublic void writeToParcel(Parcel dest, int flags){dest.writeInt(id);dest.writeString(name);dest.writeFloat(price);}public int getId(){return id;}public void setId(int id){this.id = id;}public String getName(){return name;}public void setName(String name){this.name = name;}public float getPrice(){return price;}public void setPrice(float price){this.price = price;}}

我碰到的问题就是在这里,里面的writeToParcel 和readFromParcel这两个方法写入读出的顺序一定要一样否则就会报错,我查了一下原因,真的这错误解决都解决不了,AIDL生成的接口文件里面虽然我没有去看,但是那里面错误百出。因此在写入数据读出数据是顺序一定要一致。

还有一个知识点就是Parcelable 这个接口用于实现序列化对象。

这就有的说了,对象为什么要序列化??

1.永久性保存对象,保存对象的字节序列到本地文件。
        2.通过序列化对象在网络中传递对象。
        3.通过序列化对象在进程间传递对象。

android自定义对象可序列化有两个选择一个是Serializable和Parcelable,

 二、当对象需要被序列化时如何选择所使用的接口
        1.在使用内存的时候Parcelable比Serializable的性能高。
        2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC(内存回收)。
        3.Parcelable不能使用在将对象存储在磁盘上这种情况,因为在外界的变化下Parcelable不能很好的保证数据的持续性。

下面单独写一个aidl 文件。

parcelable Product; 

MyService类

package mobile.android.complex.type.aidl;import java.util.HashMap;import java.util.Map;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException; public class MyService extends Service{ public class MyServiceImpl extends IMyService.Stub{@Overridepublic Product getProduct() throws RemoteException{Product product = new Product();product.setId(1234);product.setName("汽车");product.setPrice(31000); return product;}@Overridepublic Map getMap(String country, Product product)throws RemoteException{Map map = new HashMap<String, String>();map.put("country", country);map.put("id", product.getId());map.put("name", product.getName());map.put("price", product.getPrice());map.put("product", product);return map;}}@Overridepublic IBinder onBind(Intent intent){return new MyServiceImpl();}}

下面还是老样子来写客户端

package mobile.android.complex.type.aidlclient;import mobile.android.complex.type.aidl.IMyService;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class Main extends Activity implements OnClickListener{private IMyService myService = null;private Button btnInvokeAIDLService;private Button btnBindAIDLService;private TextView textView;private ServiceConnection serviceConnection = new ServiceConnection(){ @Overridepublic void onServiceConnected(ComponentName name, IBinder service){myService = IMyService.Stub.asInterface(service);btnInvokeAIDLService.setEnabled(true);}@Overridepublic void onServiceDisconnected(ComponentName name){// TODO Auto-generated method stub}};@Overridepublic void onClick(View view){switch (view.getId()){case R.id.btnBindAIDLService:bindService(new Intent("mobile.android.complex.type.aidl.IMyService"),serviceConnection, Context.BIND_AUTO_CREATE);break;case R.id.btnInvokeAIDLService:try{String s = "";s = "Product.id = " + myService.getProduct().getId() + "\n";s += "Product.name = " + myService.getProduct().getName()+ "\n";s += "Product.price = " + myService.getProduct().getPrice()+ "\n";s += myService.getMap("China", myService.getProduct()).toString();textView.setText(s);}catch (Exception e){}break;}}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);btnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);btnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService);btnInvokeAIDLService.setEnabled(false);textView = (TextView) findViewById(R.id.textview);btnInvokeAIDLService.setOnClickListener(this);btnBindAIDLService.setOnClickListener(this);}}

别忘了,最后应该把生成的接口文件连包一起复制到服务端。

好了,就写到这了 ,又温习了一遍。

0 0
原创粉丝点击