小菜毛毛技术分享

与大家共同成长

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

建立AIDL服务的步骤(3)

(4)编写一个MyService类,代码如下:

  1. package net.blogjava.mobile.complex.type.aidl;  
  2.  
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9. //  AIDL服务类  
  10. public class MyService extends Service  
  11. {   
  12.     public class MyServiceImpl extends IMyService.Stub  
  13.     {  
  14.         @Override  
  15.         public Product getProduct() throws RemoteException  
  16.         {  
  17.             Product product = new Product();  
  18.             product.setId(1234);  
  19.             product.setName("汽车");  
  20.             product.setPrice(31000);   
  21.             return product;  
  22.         }  
  23.         @Override  
  24.         public Map getMap(String country, Product
    product) throws RemoteException  
  25.         {  
  26.             Map map = new HashMap<String, String>();  
  27.             map.put("country", country);  
  28.             map.put("id", product.getId());  
  29.             map.put("name", product.getName());  
  30.             map.put("price", product.getPrice());  
  31.             map.put("product", product);  
  32.             return map;  
  33.         }  
  34.     }  
  35.     @Override  
  36.     public IBinder onBind(Intent intent)  
  37.     {          
  38.         return new MyServiceImpl();  
  39.     }  

(5)在AndroidManifest.xml文件中配置MyService类,代码如下:

  1. <service android:name=".MyService" > 
  2.     <intent-filter>   
  3.         <action android:name="net.blogjava.
    mobile.complex.type.aidl.IMyService"
     /> 
  4.     </intent-filter> 
  5. </service> 

在客户端调用AIDL服务的方法与实例52介绍的方法相同,首先将IMyService.java和Product.java文件复制到客户端工程(ch08_complextypeaidlclient),然后绑定AIDL服务,并获得AIDL服务对象,最后调用AIDL服务中的方法。完整的客户端代码如下:

  1. package net.blogjava.mobile;  
  2.  
  3. import net.blogjava.mobile.complex.type.aidl.IMyService;  
  4. import android.app.Activity;  
  5. import android.content.ComponentName;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.ServiceConnection;  
  9. import android.os.Bundle;  
  10. import android.os.IBinder;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15.  
  16. public class Main extends Activity implements OnClickListener  
  17. {  
  18.     private IMyService myService = null;  
  19.     private Button btnInvokeAIDLService;  
  20.     private Button btnBindAIDLService;  
  21.     private TextView textView;  
  22.     private ServiceConnection serviceConnection = new ServiceConnection()  
  23.     {  
  24.         @Override  
  25.         public void onServiceConnected(ComponentName name, IBinder service)  
  26.         {  
  27.               //  获得AIDL服务对象  
  28.             myService = IMyService.Stub.asInterface(service);  
  29.             btnInvokeAIDLService.setEnabled(true);  
  30.         }  
  31.         @Override  
  32.         public void onServiceDisconnected(ComponentName name)  
  33.         {  
  34.         }  
  35.     };  
  36.     @Override  
  37.     public void onClick(View view)  
  38.     {  
  39.         switch (view.getId())  
  40.         {  
  41.             case R.id.btnBindAIDLService:  
  42.                    //  绑定AIDL服务  
  43.                 bindService(new Intent("net.blogjava.
    mobile.complex.type.aidl.IMyService"),  
  44.                         serviceConnection, Context.BIND_AUTO_CREATE);  
  45.                 break;  
  46.             case R.id.btnInvokeAIDLService:  
  47.                 try  
  48.                 {  
  49.                     String s = "";  
  50.                        //  调用AIDL服务中的方法  
  51.                     s = "Product.id = " + myService.
    getProduct().getId() + "\n";  
  52.                     s += "Product.name = " + myService.
    getProduct().getName() + "
    \n";  
  53.                     s += "Product.price = " + myService.
    getProduct().getPrice() + "
    \n";              
  54.                     s += myService.getMap("China", 
    myService.getProduct()).toString();  
  55.                     textView.setText(s);  
  56.                 }  
  57.                 catch (Exception e)  
  58.                 {  
  59.                 }  
  60.                 break;  
  61.         }  
  62.     }  
  63.     @Override  
  64.     public void onCreate(Bundle savedInstanceState)  
  65.     {  
  66.         super.onCreate(savedInstanceState);  
  67.         setContentView(R.layout.main);  
  68.         btnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);  
  69.         btnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService);  
  70.         btnInvokeAIDLService.setEnabled(false);  
  71.         textView = (TextView) findViewById(R.id.textview);  
  72.         btnInvokeAIDLService.setOnClickListener(this);  
  73.         btnBindAIDLService.setOnClickListener(this);  
  74.     }  

首先运行服务端程序,然后运行客户端程序,单击【绑定AIDL服务】按钮,待成功绑定后,单击【调用AIDL服务】按钮,会输出如图8.27所示的内容。

 
图8.27  调用传递复杂数据的AIDL服务

posted on 2010-11-19 17:09 小菜毛毛 阅读(252) 评论(0)  编辑  收藏 所属分类: andriod

只有注册用户登录后才能发表评论。


网站导航: