绑定到正在运行的服务(在 finish() 之后)/回调处理程序

2024-03-12

又是一个关于 LocalServices 的问题。在 onDestroy() 之后,如何(重新)绑定到现有服务?

问题:我正在绑定到服务并从活动启动该服务。我将可运行对象发布到 Binder,以在 UI 上进行回调(更新进度条)。当我关闭此 Activity 时,操作系统可以结束生命周期并销毁该 Activity,调用 onDestroy(),对吧?我模拟这个,在 onPause() 方法中调用 finish() 。那么一旦我重新启动活动,如何再次绑定到相同的服务?我以为服务是 Singelton,但是当我尝试重新绑定时,我得到了另一个活页夹引用。所以binder.callbackHandler.post(binder.progressHandler);仍然引用旧的活页夹/回调/progressHandler,而不是我的新的。 甚至服务的构造函数也被再次调用!

是否有任何解决方案可以有一个进度条,通过来自服务的回调对象进行更新(工作)。关闭/onDestroy() 活动。回来,继续进度条?

我的代码相当大,但重新创建了 Szenario:

    public class MyService extends Service {
        private final LocalBinder binder = new LocalBinder();

        public class LocalBinder extends Binder implements TestRunServiceBinder {
            private Handler callbackHandler;
            private ServiceStartActivity.RunOnServiceProgress onProgress;

            @Override
            public void setActivityCallbackHandler(Handler messageHandler) {
                callbackHandler = messageHandler;
            }

            @Override
            public void setServiceProgressHandler(RunOnServiceProgress runnable) {
                onProgress = runnable;
            }

                public void doSomething(){
                     _doSomething();

        };

       private void _doSomething(){
        while(...){
           //do this a couple of times (could take up to 10min)
           binder.callbackHandler.post(binder.progressHandler);
           wait()
        }
      }

    }

_

public class ServiceStartActivity{
 private final Handler messageHandler = new Handler();
     private ServiceConnection mTestServiceConnection = new ServiceConnection() {


        @Override
        public void onServiceDisconnected(ComponentName name) {
            testRunBinder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            testRunBinder = (TestRunServiceBinder) service;
            testRunBinder.setActivityCallbackHandler(messageHandler);
            testRunBinder.setServiceProgressHandler(new RunOnServiceProgress());
        }
      };

     @Override
     protected void onStart() {
    super.onStart();

    // bind to the Service
    final Intent serviceIntent = new Intent(ServiceStartActivity.this,
            MyService.class);
    getApplicationContext().bindService(serviceIntent,
            mTestServiceConnection, Context.BIND_AUTO_CREATE);

     }
    @Override
     protected void onStop() {
    super.onStop();
    getApplicationContext().unbindService(mTestServiceConnection);
    }
       public class RunOnServiceProgress implements Runnable {
            @Override
            public void run() {
                //do something on the UI!
                    }
        }
}

我现在明白了。解决方案是显式调用startService(serviceIntent);在使用绑定到服务之前getApplicationContext().bindService(serviceIntent,mTestServiceConnection, Context.BIND_AUTO_CREATE);

Reason:当您启动服务时bindService(),它成为绑定服务

仅当另一个应用程序组件绑定到它时才运行。

如果您启动服务startService() it can

可以无限期地在后台运行,

所以如果你有例如UI 上的进度条,并且您希望它继续更新它,您应该启动您的服务,并在 onResume() / onPause() 中绑定和取消绑定它。但请注意:由于您手动启动了服务,您还应该手动停止它。最简单的方法是调用stopSelf()一旦服务完成它的工作。

该解决方案涵盖了 Activity 的正确绑定,例如即使在活动被破坏或方向改变后,同一服务的进度条也会显示出来。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绑定到正在运行的服务(在 finish() 之后)/回调处理程序 的相关文章

随机推荐