android--service之绑定服务交互

上传人:wt****50 文档编号:32820285 上传时间:2018-02-12 格式:DOC 页数:31 大小:779.50KB
返回 下载 相关 举报
android--service之绑定服务交互_第1页
第1页 / 共31页
android--service之绑定服务交互_第2页
第2页 / 共31页
android--service之绑定服务交互_第3页
第3页 / 共31页
android--service之绑定服务交互_第4页
第4页 / 共31页
android--service之绑定服务交互_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《android--service之绑定服务交互》由会员分享,可在线阅读,更多相关《android--service之绑定服务交互(31页珍藏版)》请在金锄头文库上搜索。

1、前言开篇名义,这篇博客介绍一下 Android 下使用绑定服务进行时数据交互的几种方法。关于Android 下 Service 的内容,前面两篇博客已经介绍了,不清楚的可以移步过去先看看:Android-Service 之基础、Android-Service 之提高。在前面的博客中已经介绍到了,对于 Service 组件而言,它只有在绑定模式下才可以与客户端进行时交互,这里讲解几个方法进行绑定服务与客户端间的交互方法:1. 使用 IBinder 接口 2. 使用 Messenger 类 3. 使用 AIDL 虽然根据官方文档给出了三个方法,其中 AIDL 涉及的内容超出本博客内容范围,以后有机

2、会在另外介绍,本篇博客只介绍 1、2 两种方式的数据交互。使用 IBinder 接口如果看了之前关于 Service 博客的人,应该对 IBinder 接口有所了解,这里简单介绍一下IBinder。IBinder 是远程对象的基本接口,是为高性能而设计的轻量级远程调用机制的核心部分。但它不仅用于远程调用,也可以用于进程内调用。这个接口定义了与远程对象交互的协议,一般不直接实现这个接口,而是从它的实现类 Binder 中继承。通过 IBinder 进行服务的交互一般有两种方式,一种方式是使用 IBinder.transact()方法向远端的 IBinder 对象发送一个发出调用,会回调远端的 B

3、inder.onTransact()方法,这个方法传递的数据是 Parcel。Parcel 是一种缓冲区,除了数据外还有有一些描述它内容的元素,如果查看源码的话会发现,Parcel 本质上是一个 Serialize,只是它在内存中完成了序列化和反序列化,利用的是连续的内存空间,因此效率会更高,并且 AIDL 的数据也是通过 Parcel 来交互的。另外一种方法就是抛弃 IBinder 中原生的方法,使用自定义的接口方法进行数据交互,这也是 Android 官方推荐绑定服务的一种数据交互方式。当然,不管是使用 transact()给远程服务交互,还是使用自定义的接口交互,都是同步执行的,直到远程

4、服务执行完并返回结果才会继续向下执行。其他关于适应 IBinder 服务的内容,在博客 Android-Service 之基础中已经讲解过了,这里不再累述。下面使用一个例子来演示一下使用自定义接口与服务进行交互的例子。服务:IBinderSer.java1 package cn.bgxt.servicebinddatedemo;2 3 import android.app.Service;4 import android.content.Intent;5 import android.os.Binder;6 import android.os.IBinder; 7 import android

5、.os.Parcel;8 import android.os.RemoteException;9 import android.util.Log;10 http:/ 11 public class IBinderSer extends Service 12 private final String TAG=main;13 private final int MULTIPLE=1024; 14 public final IBinder mBinder=new LocalBinder();15 16 public class LocalBinder extends Binder17 / 在 Bin

6、der 中定义一个自定义的接口用于数据交互18 / 这里直接把当前的服务传回给宿主19 public IBinderSer getService()20 return IBinderSer.this;21 22 23 24 Override25 public IBinder onBind(Intent intent) 26 Log.i(TAG, The service is binding!);27 / 绑定服务,把当前服务的 IBinder 对象的引用传递给宿主28 return mBinder;29 30 31 public int getMultipleNum(int num)32 /

7、定义一个方法 用于数据交互33 return MULTIPLE*num;34 35 调用服务的 Activity:IBinderActivity.java1 package cn.bgxt.servicebinddatedemo;2 3 import android.app.Activity;4 import android.app.Service;5 import android.content.ComponentName;6 import android.content.Intent;7 import android.content.ServiceConnection;8 import a

8、ndroid.os.Bundle;9 import android.os.IBinder;10 import android.os.Messenger;11 import android.os.Parcel;12 import android.os.RemoteException;13 import android.view.View;14 import android.widget.Button;15 import android.widget.Toast;16 http:/ 17 public class IBinderActivity extends Activity 18 privat

9、e Button btnStart, btnInvoke, btnStop;19 IBinderSer mService=null;20 private ServiceConnection mConnection = new ServiceConnection() 21 22 Override23 public void onServiceDisconnected(ComponentName name) 24 mService = null;25 26 27 Override28 public void onServiceConnected(ComponentName name, IBinde

10、r service) 29 / 获取服务上的 IBinder 对象,调用 IBinder 对象中定义的自定义方法,获取 Service 对象30 IBinderSer.LocalBinder binder=(IBinderSer.LocalBinder)service;31 mService=binder.getService();32 33 ;34 35 Override36 protected void onCreate(Bundle savedInstanceState) 37 super.onCreate(savedInstanceState);38 setContentView(R.

11、layout.layout_service);39 btnStart = (Button) findViewById(R.id.btnStartSer);40 btnInvoke = (Button) findViewById(R.id.btnInvokeMethod);41 btnStop = (Button) findViewById(R.id.btnStopSer);42 43 btnStart.setOnClickListener(onclick);44 btnInvoke.setOnClickListener(onclick);45 btnStop.setOnClickListene

12、r(onclick);46 47 48 View.OnClickListener onclick = new View.OnClickListener() 49 50 Override51 public void onClick(View v) 52 switch (v.getId() 53 case R.id.btnStartSer:54 Toast.makeText(getApplicationContext(), 绑定服务成功, Toast.LENGTH_SHORT).show();55 bindService(new Intent(IBinderActivity.this,IBinde

13、rSer.class),mConnection,Service.BIND_AUTO_CREATE); 56 break;57 case R.id.btnInvokeMethod:58 if(mService=null)59 Toast.makeText(getApplicationContext(), 请先绑定服务 , Toast.LENGTH_SHORT).show();60 return;61 62 / 调用绑定服务上的方法,进行数据交互63 int iResult=mService.getMultipleNum(10);64 Toast.makeText(getApplicationContext(), 服务计算结果为:+iResult, Toast.LENGTH_SHORT).show();65 break;66 case R.id.btnStopSer:67 Toast.makeText(getApplicationContext(), 服务解除绑定, Toast.LENGTH_SHORT).show();68 unbindService(mConnection);69 mService=null;70 break;71 default:72 break;73 74 75 ;76 执行结果:nt

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 机械/制造/汽车 > 机械理论及资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号