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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android线程间通信的几种方法_Android线程间通信机制

    android线程间通信的几种方法_Android线程间通信机制讲解Handler机制的博文很多,我也看了很多,但说实话,在我对Handler几乎不怎么了解的情况下,每一篇文章我都没太看懂,看完之后脑子里还是充满了疑问。究其原因,是因为几乎每一篇文章一上来就开始深入Handler源码,使得在一些宏观的问题上还是充满疑问,如果你从来没接触过Handler,对一些基础的问题还充满疑问,那深入源码去探究根源肯定会有些吃力。下面,我就从一个初学者思考的角度,来讲一讲H…

    2022年10月7日
    0
  • sqlserver 动态sql执行execute和sp_executesql

    sqlserver 动态sql执行execute和sp_executesqlsqlserver动态sql的执行,有两个方法execute和sp_executesql.其中第一个方法execute可以简写为exec.execute方法适合执行没有返回值的动态sql,sp_executesql可以获取到动态sql的返回值.二者比较起来,前者写起来简单,后者功能强大些,但写起来麻烦,使用的时候具体情况具体分析吧.  在function中不能使用exec和sp_exec

    2022年5月22日
    36
  • IDEA 主题下载

    IDEA 主题下载IDEA中主题可以更换,大家可以直接到http://www.riaway.com/网站或http://color-themes.com/?view=index网站,直接下载自己喜欢的主题。然后导入进去IDEA中,IDEA中代码编辑器和控制台的字体颜色和背景就会发生改变。这些主题导入IDEA之后,如果对某些个字体颜色配色方案不满意的,还可以在IDEA中修改设置,很人性化转载…

    2022年5月6日
    327
  • python类的初始化方法_python初始化列表

    python类的初始化方法_python初始化列表【背景】在scikit-learn基础上系统结合数学和编程的角度学习了机器学习后(我的github:https://github.com/wwcom614/machine-learning),意犹未尽,打算再借势学习下深度学习TensorFlow。无奈安装之后遇到了这个问题,耽误了几个小时才得以解决。我发现这是个很多人开始TensorFlow之旅普遍遇到的问题,而且是很多人尝试了网上很多方法都未解…

    2022年8月30日
    1
  • Idea激活码最新教程2023.2.7版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2023.2.7版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2023 2 7 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2023 2 7 成功激活

    2025年5月28日
    0
  • 偶然发现关于网页JavaScript脚本无法正常运行的原因

    偶然发现关于网页JavaScript脚本无法正常运行的原因

    2022年2月4日
    40

发表回复

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

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