android学习之绑定Service并与之通信
2016-04-22 20:18
459 查看
这篇文章的代码来自疯狂讲义
本来打算今天写一篇关于aidl的文章的,但是感觉网上的代码都不是很能看懂,然后因为涉及到bindservice,然后就先写一篇bindservice的文章,稍后再添上使用aidl的关于进程间通信的文章
如果Service和访问者之间需要进行方法的调用或者交换数据,那么久应该使用bindservice和unbindservice的方法启动,关闭service。
Context的bindService()方法的完整方法的三个参数意义:
第一个:Service,通过该参数指定要启动的Service
第二个:conn,该参数的第一个是ServiceCoonnection对象,该对象用于监听访问者与Service之间的连接情况。当访问者与Sercvice之间连接成功的时候将回调该ServiceConnection对象的OnServiceConnected(),当Service所在进程因为异常终止或者其它原因终止,那么将回掉ServiceConnection对象的onServiceDisconnected(),这里的函数的参数我就没有打了。
需要注意:当调用者主动通过unBindService()方法断开Service的链接时,onServiceDisconnected不会被调用。
第三个参数:flags,指定绑定的时候是否自动创建Service,如果为0则不自动创建,或BIND_AUTO_CREATE(自动创建)
onServiceConnected()方法中有一个Ibinder对象,这个对象可以进行被绑定的Service之间的通信
主要是通过onBinder对象返回的Ibinder对象进行通信。
这里,我会画一个图来解释这个问题:
![](http://img.blog.csdn.net/20160422200659510?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
找个时间真的要研究一下这个画图应该怎么处理。
好吧,终于可以贴代码了:
本来打算今天写一篇关于aidl的文章的,但是感觉网上的代码都不是很能看懂,然后因为涉及到bindservice,然后就先写一篇bindservice的文章,稍后再添上使用aidl的关于进程间通信的文章
如果Service和访问者之间需要进行方法的调用或者交换数据,那么久应该使用bindservice和unbindservice的方法启动,关闭service。
Context的bindService()方法的完整方法的三个参数意义:
第一个:Service,通过该参数指定要启动的Service
第二个:conn,该参数的第一个是ServiceCoonnection对象,该对象用于监听访问者与Service之间的连接情况。当访问者与Sercvice之间连接成功的时候将回调该ServiceConnection对象的OnServiceConnected(),当Service所在进程因为异常终止或者其它原因终止,那么将回掉ServiceConnection对象的onServiceDisconnected(),这里的函数的参数我就没有打了。
需要注意:当调用者主动通过unBindService()方法断开Service的链接时,onServiceDisconnected不会被调用。
第三个参数:flags,指定绑定的时候是否自动创建Service,如果为0则不自动创建,或BIND_AUTO_CREATE(自动创建)
onServiceConnected()方法中有一个Ibinder对象,这个对象可以进行被绑定的Service之间的通信
主要是通过onBinder对象返回的Ibinder对象进行通信。
这里,我会画一个图来解释这个问题:
找个时间真的要研究一下这个画图应该怎么处理。
好吧,终于可以贴代码了:
package com.jk.bindservice; import com.jk.bindservice.BindService.MyBinder; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { // declare three button Button btn_bind, btn_unbind, btn_get; // declare a binder to receive the binder BindService.MyBinder binder; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); innit(); } private void innit() { // bundle the button with id btn_bind = (Button) findViewById(R.id.btn_bindservice); btn_unbind = (Button) findViewById(R.id.btn_unbindservice); btn_get = (Button) findViewById(R.id.btn_getconservice); // set the clicklistener btn_bind.setOnClickListener(this); btn_unbind.setOnClickListener(this); btn_get.setOnClickListener(this); // set the intent intent = new Intent(this, BindService.class); } private ServiceConnection conn = new ServiceConnection() { // 当Activity和Service连接成功时回调该方法 // invoke the method when the activity connect the service @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("service connected"); // get the mybinder that return by service's onBind // 获取Service的onBind方法所返回的MyBinder对象 binder = (MyBinder) service; } // invoke the method when disconnected with service // 当连接断开时调用该方法 @Override public void onServiceDisconnected(ComponentName name) { System.out.println("service disconnected"); } }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_bindservice: // bind the service // 绑定到指定的Service bindService(intent, conn, Service.BIND_AUTO_CREATE); break; case R.id.btn_unbindservice: // unbindservice // 解除绑定 unbindService(conn); break; case R.id.btn_getconservice: // get the binder data Toast.makeText(this, "count" + binder.getCount(), Toast.LENGTH_SHORT).show(); break; } } }
package com.jk.bindservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class BindService extends Service { private int count; private boolean quit; // define the object that return by onBinder // 定义onBinder方法所返回的对象 private MyBinder binder = new MyBinder(); // extends binder to realize Ibinder // 通过继承Binder来实现IBinder类 public class MyBinder extends Binder { public int getCount() { // 获取service的运行的状态:count return count; } } // we can sent message by this method // 必须实现的方法,绑定该Service时回调改方法 @Override public IBinder onBind(Intent arg0) { System.out.println("service is binded"); // 返回Ibinder对象 return binder; } // invoke this method when service was created // service 被创建时调用该方法 @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created"); // launch a thread to change the count // 启动一条线程,动态修改count状态值 new Thread() { public void run() { while (!quit) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } count++; } }; }.start(); } // invoke this method when service was connetted // service 被断开时回掉该方法 @Override public boolean onUnbind(Intent intent) { System.out.println("service is unbind"); return true; } // invoke this method when service was close // service 被关闭之前回调该方法 @Override public void onDestroy() { super.onDestroy(); this.quit = true; System.out.println("service is destroyed"); } }
相关文章推荐
- 不翻墙,利用国内源下载Android 源码
- Android中Scrollview、ViewPager、ListView冲突问题 (亲测可用)
- Android ecludeFromRecents
- Android 程序进入后台 恢复到前台
- Android IntentService完全解析 当Service遇到Handler
- Android HandlerThread 完全解析
- Android保存用户名等小数据使用SharedPreference
- Android生成一维码
- android实现qq邮箱多个图标效果
- Android常见问题总结(一)
- Android Studio 获取应用的 sha1 值
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android中一些提示的实现方法
- Android 动画应用大集合
- Android Studio的使用03-配置忽略文件
- Android群英传第五章笔记·Android Scroll分析
- android源码解析之(十三)-->apk安装流程
- android的消息机制
- 天气渐热,来片雪花降降温——Android自定义SurfaceView实现雪花效果
- android 控件 下拉刷新 SwipeRefreshLayout