Android开发:bindService的使用方法

Android开发:bindService的使用方法http://blog.csdn.net/zhou_wenchong/article/details/51302574bindService用于绑定一个服务。这样当bindService(intent,conn,flags)后,就会绑定一个服务。这样做可以获得这个服务对象本身,而用startService(intent)的方法只能启动服务。   bindService方式的一般过程:

大家好,又见面了,我是你们的朋友全栈君。

http://blog.csdn.net/zhou_wenchong/article/details/51302574

bindService用于绑定一个服务。这样当bindService(intent,conn,flags)后,就会绑定一个服务。这样做可以获得这个服务对象本身,而用startService(intent)的方法只能启动服务。

    bindService方式的一般过程:

1.新建Service类BindService。在BindService类里新建内部类MyBinder,继承自Binder(Binder实现IBinder接口)。MyBinder提供方法返回BindService实例。

    public class MyBinder extends Binder{

        
        
public BindService getService(){

            
return BindService.this;
        }
    }

实例化MyBinder得到mybinder对象;

重写onBind()方法:

 @Override

 public IBinder onBind(Intent intent) {

  return mybinder;
 }

2.在Activity里,实例化ServiceConnection接口的实现类,重写onServiceConnected()和onServiceDisconnected()方法

ServiceConnection conn=new ServiceConnection(){

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {

  }

  @Override
  public void onServiceDisconnected(ComponentName name) {

  }

};

3.在Activity的onCreate()方法里,新建Intent,并绑定服务

        Intent intent=new Intent(MainActivity.this,BindService.class); 


        bindService(intent, conn,BIND_AUTO_CREATE);

 

4.在Activity的onDestroy里面,添加

unbindService(conn);

如果不加这一步,就会报Android.app.ServiceConnectionLeaked: ******.MainActivity has leaked ServiceConnection的异常。

 

bindService()的执行过程如下:

bindService(intent,conn,flag)->Service:onCreate()->Service:onBind()->Activity:onServiceConnected()

code

[java] 
view plain  
copy

  1.  1:调用者  
  2.   
  3. package com.zhf.local;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.ServiceConnection;  
  10. import android.os.Bundle;  
  11. import android.os.IBinder;  
  12.   
  13. /** 
  14.  * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量 
  15.  *  
  16.  * @author Administrator 
  17.  *  
  18.  */  
  19. public class LocalServiceActivity extends Activity {  
  20.     /** Called when the activity is first created. */  
  21.     private MyService myService;  
  22.   
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.   
  28.         Intent intent = new Intent(this, MyService.class);  
  29.         bindService(intent, connection, Context.BIND_AUTO_CREATE);  
  30.     }  
  31.   
  32.     private ServiceConnection connection = new ServiceConnection() {  
  33.   
  34.         @Override  
  35.         public void onServiceDisconnected(ComponentName name) {  
  36.             myService = null;  
  37.         }  
  38.   
  39.         @Override  
  40.         public void onServiceConnected(ComponentName name, IBinder service) {  
  41.             myService = ((MyService.MyBinder) service).getService();  
  42.             System.out.println(“Service连接成功”);  
  43.             // 执行Service内部自己的方法  
  44.             myService.excute();  
  45.         }  
  46.     };  
  47.   
  48.     protected void onDestroy() {  
  49.         super.onDestroy();  
  50.         unbindService(connection);  
  51.     };  
  52. }  
  53.   
  54. 2:服务者  
  55.   
  56. package com.zhf.local;  
  57.   
  58. import android.app.Service;  
  59. import android.content.Intent;  
  60. import android.os.Binder;  
  61. import android.os.IBinder;  
  62.   
  63. public class MyService extends Service {  
  64.     private final IBinder binder = new MyBinder();  
  65.   
  66.     @Override  
  67.     public IBinder onBind(Intent intent) {  
  68.         return binder;  
  69.     }  
  70.   
  71.     public class MyBinder extends Binder {  
  72.         MyService getService() {  
  73.             return MyService.this;  
  74.         }  
  75.     }  
  76.   
  77.     public void excute() {  
  78.         System.out.println(“通过Binder得到Service的引用来调用Service内部的方法”);  
  79.     }  
  80.   
  81.     @Override  
  82.     public void onDestroy() {  
  83.         // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用  
  84.         super.onDestroy();  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean onUnbind(Intent intent) {  
  89.         // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用  
  90.         System.out.println(“调用者退出了”);  
  91.         return super.onUnbind(intent);  
  92.     }  
  93. }  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133436.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux clion激活码_通用破解码

    linux clion激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    339
  • Java四舍五入计算

    Java四舍五入计算java四舍五入计算。

    2022年5月11日
    30
  • 递归函数[通俗易懂]

    递归函数[通俗易懂]如果一个函数在内部调用自身,这个函数就叫做递归函数递归函数的简单定义如下:这只是一个简单的定义,什么也做不了。当然,你可以尝试会发生什么结果,理论上会永远运行下去,但实际操作时发现不一会儿程序就

    2022年7月1日
    35
  • Java线程(二):线程同步synchronized和volatile

    Java线程(二):线程同步synchronized和volatile要说明线程同步问题首先要说明Java线程的两个特性,可见性和有序性。多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变量来实现。拿上篇博文中的例子来说明,在多个线程之间共享了Count类的一个对象,这个对象是被创建在主内存(堆内存)中,每个线程都有自己的工作内存(线程栈),工作内存存储了主内存Count对象的一个副本,当线程操作Count对象时,首先从主内存复制Co…

    2022年7月15日
    16
  • oracle隐式转换_oracle查看游标数量

    oracle隐式转换_oracle查看游标数量原文地址:http://blog.itpub.net/29324876/viewspace-1096741/1     Oracle 隐式转换Oracle中对不同类型的处理具有显式类型转换(Explicit)和隐式类型转换(Implicit)两种方式,对于显式类型转换,我们是可控的,但是对于隐式类型转换,当然不建议使用,因为很难控制,有不少缺点,但是我们很难避免

    2022年10月11日
    3
  • 用keras搭建一个简单的一维卷积神经网络

    用keras搭建一个简单的一维卷积神经网络编程环境:python3.6.8tensorflow1.12.3点击下载离线包matplotlib3.1.2numpy1.17.4数据集说明:我所采用的数据集,是我自己构建的一个网络流量数据集,借鉴了WeiWang等人端到端的思想,但是处理成的数据集却不同于他们的MNIST型数据集,而是采用的npy进行存储。由于只是用于测试模型搭建,该数据集仅包含了一部…

    2022年9月22日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号