发布时间:2025-12-09 13:48:38 浏览次数:4
一直使用 Qt 来进行桌面端和移动端开发,对 Android 开发并不熟悉,但是项目中往往又经常会用到 Android 原生开发的东西。本次收集了一些关于BindService相关的知识供自己学习和参考。
bindService介绍
bindService是绑定Service服务,执行service服务中的逻辑流程。
service通过Context.startService()方法开始,通过Context.stopService()方法停止;也可以通过Service.stopSelf()方法或者Service.stopSelfResult()方法来停止自己。只要调用一次stopService()方法便可以停止服务,无论之前它被调用了多少次的启动服务方法。
客户端建立一个与Service的连接,并使用此连接与Service进行通话,通过Context.bindService()方法来绑定服务,Context.unbindService()方法来关闭服务。多个客户端可以绑定同一个服务,如果Service还未被启动,bindService()方法可以启动服务。
上面startService()和bindService()两种模式是完全独立的。你可以绑定一个已经通过startService()方法启动的服务。例如:一 个后台播放音乐服务可以通过startService(intend)对象来播放音乐。可能用户在播放过程中要执行一些操作比如获取歌曲的一些信息,此时 activity可以通过调用bindServices()方法与Service建立连接。这种情况下,stopServices()方法实际上不会停止 服务,直到最后一次绑定关闭。
如果没有程序停止它或者它自己停止,service将一直运行。在这种模式 下,service开始于调用Context.startService() ,停止于Context.stopService(). service可以通过调用Android Service 生命周期() 或 Service.stopSelfResult()停止自己。不管调用多少次startService() ,只需要调用一次 stopService() 就可以停止service。
可以通过接口被外部程序调用。外部程序建立到service的连接,通过 连接来操作service。建立连接调开始于Context.bindService(), 结束于Context.unbindService(). 多个客户端可以绑定到同一个service,如果service没有启动, bindService() 可以选择启动它。
这2种模式不是完全分离的。你可以可以绑定到一个通过startService()启动的服务。如 一个intent想要播放音乐,通过startService() 方法启动后台播放音乐的service。然后,也许用户想要操作播放器或者获取当前正在播放的乐曲的信息,一个activity就会通过 bindService()建立一个到此service的连接. 这种情况下 stopService() 在全部的连接关闭后才会真正停止service。
context.bindService() ——> onCreate() ——> onBind() ——> Service running ——> onUnbind() ——> onDestroy() ——> Service stop
onBind()将返回给 客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service的实例、运行状态或其他操作。这个时候把调用者 (Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用 onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate –> onBind(只一次,不可多次绑定) –> onUnbind –> onDestory。
在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
像一个activity那样,一个service有些可以用来改变状态的生命周期方法,但是比activity的方法少,service生命周期方法只有三个public
void onCreate()void onStart(Intent intent)void onDestroy()通过实现这三个生命周期方法,你可以监听service的两个嵌套循环的生命周期:
service 的整个生命周期是在onCreate()和onDestroy()方法之间。和activity一样,在onCreate()方法里初始化,在 onDestroy()方法里释放资源。例如,一个背景音乐播放服务可以在onCreate()方法里播放,在onDestroy()方法里停止。
service 的活动生命周期是在onStart()之后,这个方法会处理通过startServices()方法传递来的Intent对象。音乐service可以通 过开打intent对象来找到要播放的音乐,然后开始后台播放。注: service停止时没有相应的回调方法,即没有onStop()方法,只有onDestroy()销毁方法。
任何服务无论它怎样建立,默认客户端都可以连接,所以任何service都能够接收onBind()和onUnbind()方法
onCreate()方法和onDestroy()方法是针对所有的services,无论它们是否启动,通过Context.startService()和Context.bindService()方法都可以访问执行。然而,只有通过startService()方法启动service服务时才会调用onStart()方法。
如果一个service允许别人绑定,那么需要实现以下额外的方法:
IBinder onBind(Intent intent)boolean onUnbind(Intent intent)void onRebind(Intent intent)onBind()回调方法会继续传递通过bindService()传递来的intent对象
onUnbind()会处理传递给unbindService()的intent对象。如果service允许绑定,onBind()会返回客户端与服务互相联系的通信句柄(实例)。
如果建立了一个新的客户端与服务的连接,onUnbind()方法可以请求调用onRebind()方法。
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()1:调用者 package com.zhf.local; 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; /** * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量 * * @author Administrator * */ public class LocalServiceActivity extends Activity { /** Called when the activity is first created. */ private MyService myService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(this, MyService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { myService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = ((MyService.MyBinder) service).getService(); System.out.println("Service连接成功"); // 执行Service内部自己的方法 myService.excute(); } }; protected void onDestroy() { super.onDestroy(); unbindService(connection); }; } 2:服务者 package com.zhf.local; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { private final IBinder binder = new MyBinder(); @Override public IBinder onBind(Intent intent) { return binder; } public class MyBinder extends Binder { MyService getService() { return MyService.this; } } public void excute() { System.out.println("通过Binder得到Service的引用来调用Service内部的方法"); } @Override public void onDestroy() { // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用 super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { // 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用 System.out.println("调用者退出了"); return super.onUnbind(intent); } } 在清单文件中注册
<service android:name=".MyService"> <intent-filter> <action android:name="com.zhf.local"/> </intent-filter></service>原文地址:
https://www.cnblogs.com/wicrecend/p/5288527.html
https://blog.csdn.net/zhou_wenchong/article/details/51302574