• Android四大组件之Service(二)


    上一篇文章讲了启动方式为startService

    有兴趣的同学可移步前去学习

    Android四大组件之Service(一)_水很清的博客-CSDN博客

    本篇文章继续往下讲,讲通过bindService来启动Service

    先看看bindService()方法的参数

    bindService(Intent service,ServiceConnection conn,int flags);

    service:该参数通过Intent指定需要启动的Service

    conn:该参数是ServiceConnnection对象,当绑定成功后,系统将调用serviceConnnection的onServiceConnected ()方法,当绑定意外断开后,系统将调用ServiceConnnection中的onServiceDisconnected方法。

    flags:该参数指定绑定时是否自动创建Service。如果指定为BIND_AUTO_CREATE,则自动创建,指定为0,则不自动创建。

    绑定方式中,当调用者通过bindService()函数绑定Service时,onCreate()函数和onBinde ( )函数将被先后调用。

    通过该方式启动Service,访问者与Service绑定在一起,访问者一旦退出了,Service也就终止了。

    取消绑定则调用unbindService()方法,并将ServiceConnnection传递给unbindService()方法。

    unbindService()方法成功后,系统并不会调用onServiceConnected(),因为onServiceConnected()仅在意外断开绑定时才被调用。

    当调用者通过unbindService()函数取消绑定Service时,onUnbind()函数将被调用。如果onUnbind()函数返回true,则表示重新绑定服务时,onRebind ()函数将被调用。

    看下bindService启动时Service的生命周期

    bindService()启动流程: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在一个生命周期中只能被调用一次。

    下面是demo:

    1. public class BindService extends Service {
    2. //声明IBinder接口的一个接口变量mBinder
    3. public final IBinder mBinder = new LocalBinder();
    4. //LocalBinder是继承Binder的一个内部类
    5. public class LocalBinder extends Binder {
    6. public BindService getService() {
    7. return BindService.this;
    8. }
    9. }
    10. @Override
    11. public void onCreate() {
    12. mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    13. MLog.e(getClass().getName(), "onCreate");
    14. }
    15. @Override
    16. public void onDestroy() {
    17. MLog.e(getClass().getName(), "onDestroy");
    18. mNM.cancel(NOTIFICATION);
    19. Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    20. }
    21. @Override
    22. public IBinder onBind(Intent intent) {
    23. MLog.e(getClass().getName(), "onBind");
    24. return mBinder;
    25. }
    26. @Override
    27. public boolean onUnbind(Intent intent) {
    28. MLog.e(getClass().getName(), "onUnbind");
    29. return super.onUnbind(intent);
    30. }
    31. }

    AndroidManifest.xml中声明

    ".service.BindService"/>

     Activity中使用,两个按钮,一个绑定,一个解绑,

    1. findViewById(R.id.btn_bind).setOnClickListener(new View.OnClickListener() {
    2. @Override
    3. public void onClick(View v) {
    4. if (!isBind) {
    5. Intent intentBind = new Intent(ServiceActivity.this, BindService.class);
    6. bindService(intentBind, serviceConnection, Context.BIND_AUTO_CREATE);
    7. isBind = true;
    8. }
    9. }
    10. });
    11. findViewById(R.id.btn_unbing).setOnClickListener(new View.OnClickListener() {
    12. @Override
    13. public void onClick(View v) {
    14. if (isBind) {
    15. isBind = false;
    16. unbindService(serviceConnection);
    17. bindService = null;
    18. }
    19. }
    20. });
    21. private ServiceConnection serviceConnection = new ServiceConnection() {
    22. @Override
    23. public void onServiceConnected(ComponentName name, IBinder service) {
    24. MLog.e(getClass().getName(), "onServiceConnected");
    25. bindService = ((BindService.LocalBinder) service).getService();
    26. }
    27. @Override
    28. public void onServiceDisconnected(ComponentName name) {
    29. MLog.e(getClass().getName(), "onServiceDisconnected");
    30. bindService = null;
    31. }
    32. };

  • 相关阅读:
    SpringBoot集成Spring Data JPA项目实操
    人工智能时代:深入了解与学以致用的智能科技
    【文件同步和备份软件】上海道宁为您带来GoodSync软件,让您轻松备份和同步您的文件
    郴州等保测评中心电话是多少?在哪里?
    我想开发一个小程序,大概需要多少钱?
    云栖大会“云计算加速开源创新论坛” 揭晓 2022 年度开源人物
    【Python 常用脚本及命令系列 12.1 -- OpenCV 设置图片区域为某个颜色】
    异步 PHP — 多进程、多线程和协程
    Spring.事务实现方式和源码分析
    Java 第三阶段增强分析需求,代码实现能力【正则表达式】
  • 原文地址:https://blog.csdn.net/taoyuxin1314/article/details/126269184