建立AIDL服务的步骤(3)
(4)编写一个MyService类,代码如下:
- package net.blogjava.mobile.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;
- // AIDL服务类
- public class MyService extends Service
- {
- public class MyServiceImpl extends IMyService.Stub
- {
- @Override
- public Product getProduct() throws RemoteException
- {
- Product product = new Product();
- product.setId(1234);
- product.setName("汽车");
- product.setPrice(31000);
- return product;
- }
- @Override
- public 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;
- }
- }
- @Override
- public IBinder onBind(Intent intent)
- {
- return new MyServiceImpl();
- }
- }
(5)在AndroidManifest.xml文件中配置MyService类,代码如下:
- <service android:name=".MyService" >
- <intent-filter>
- <action android:name="net.blogjava.
mobile.complex.type.aidl.IMyService" />
- </intent-filter>
- </service>
在客户端调用AIDL服务的方法与实例52介绍的方法相同,首先将IMyService.java和Product.java文件复制到客户端工程(ch08_complextypeaidlclient),然后绑定AIDL服务,并获得AIDL服务对象,最后调用AIDL服务中的方法。完整的客户端代码如下:
- package net.blogjava.mobile;
-
- import net.blogjava.mobile.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()
- {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- // 获得AIDL服务对象
- myService = IMyService.Stub.asInterface(service);
- btnInvokeAIDLService.setEnabled(true);
- }
- @Override
- public void onServiceDisconnected(ComponentName name)
- {
- }
- };
- @Override
- public void onClick(View view)
- {
- switch (view.getId())
- {
- case R.id.btnBindAIDLService:
- // 绑定AIDL服务
- bindService(new Intent("net.blogjava.
mobile.complex.type.aidl.IMyService"),
- serviceConnection, Context.BIND_AUTO_CREATE);
- break;
- case R.id.btnInvokeAIDLService:
- try
- {
- String s = "";
- // 调用AIDL服务中的方法
- 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;
- }
- }
- @Override
- public 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);
- }
- }
首先运行服务端程序,然后运行客户端程序,单击【绑定AIDL服务】按钮,待成功绑定后,单击【调用AIDL服务】按钮,会输出如图8.27所示的内容。
|
图8.27 调用传递复杂数据的AIDL服务
|