结合运行Service(后完成())/回调处理程序 [英] binding to running Service (after finish() ) / callback Handler

查看:209
本文介绍了结合运行Service(后完成())/回调处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同样一个关于本地服务的问题。我如何(重新)绑定到现有服务,之后的onDestroy()?

的问题: 我绑定到服务,并开始从活动服务。我张贴Runnable对象到活页夹,回调(更新进度)上的用户界面。当我关闭这个活动,操作系统可能结束生命周期和销毁活动,呼吁的onDestroy(),对不对?我模拟这个,调用完成()中的onPause()方法。所以一旦我重新启动该活动,怎么我绑定到相同的服务了吗?我认为,服务是Singelton,但是当我试图重新绑定,我得到另一种粘合剂的参考。因此, binder.callbackHandler.post(binder.progressHandler); 仍具有参考旧粘合剂/回调/ progressHandler,不是我的新的。 连服的构造函数被再次调用!

有没有什么解决办法有一个进度条,得到更新由回调对象的服务(工作)。关闭/的onDestroy()的活动。回来,然后继续进度?

我的code是相当大的,但重建的Szenario:

 公共类的MyService延伸服务{
        私人最终LocalBinder粘合剂=新LocalBinder();

        公共类LocalBinder扩展粘合剂实现TestRunServiceBinder {
            私人处理器的callbackHandler;
            私人ServiceStartActivity.RunOnServiceProgress onProgress;

            @覆盖
            公共无效setActivityCallbackHandler(处理器的MessageHandler){
                的callbackHandler =的MessageHandler;
            }

            @覆盖
            公共无效setServiceProgressHandler(RunOnServiceProgress可运行){
                onProgress =可运行;
            }

                公共无效DoSomething的(){
                     _干点什么();

        };

       私人无效_doSomething(){
        而(...){
           //这样做几次(可能需要长达10分钟)
           binder.callbackHandler.post(binder.progressHandler);
           等待()
        }
      }

    }
 

_

 公共类ServiceStartActivity {
 私人最终处理程序的MessageHandler =新的处理程序();
     私人ServiceConnection mTestServiceConnection =新ServiceConnection(){


        @覆盖
        公共无效onServiceDisconnected(组件名名){
            testRunBinder = NULL;
        }

        @覆盖
        公共无效onServiceConnected(组件名名称,服务的IBinder){
            testRunBinder =(TestRunServiceBinder)服务;
            testRunBinder.setActivityCallbackHandler(MessageHandler的);
            testRunBinder.setServiceProgressHandler(新RunOnServiceProgress());
        }
      };

     @覆盖
     保护无效的OnStart(){
    super.onStart();

    //绑定到服务
    最终意图serviceIntent =新的意图(ServiceStartActivity.this,
            MyService.class);
    getApplicationContext()。bindService(serviceIntent,
            mTestServiceConnection,Context.BIND_AUTO_CREATE);

     }
    @覆盖
     保护无效的onStop(){
    super.onStop();
    。getApplicationContext()unbindService(mTestServiceConnection);
    }
       公共类RunOnServiceProgress实现Runnable {
            @覆盖
            公共无效的run(){
                //做一些事情上的用户界面!
                    }
        }
}
 

解决方案

我现在

。解决的办法是显式调用 startService(serviceIntent); 您使用绑定到服务之前, getApplicationContext()bindService(serviceIntent,mTestServiceConnection,Context.BIND_AUTO_CREATE );

原因::当你开始使用服务bindService(),就变成了绑定服务的

  

只运行只要其他应用程序组件绑定到它。

如果你以一个服务 startService()

  

可以在后台运行下去,

所以,如果你有例如一个progessbar上的用户界面,并且希望它继续更新,你应该开始你的服务,并绑定与undbind它onResume()/的onPause()。但要carfull:既然你手动启动该服务,您也应该停止手动。要做到这一点最简单的方法是调用 stopSelf()一旦业务做了它的工作。

该soultion涵盖如与一个活动的适当结合一个progresss条相同的服务,即使该活动被破坏或改变方向时后。

Again a question about LocalServices. How do I (re-)bind to an existing Service, after onDestroy()?

The Problem: I'm binding to a Service and Starting the service from an Activity. I'm Posting runnable Objects to the Binder, for a callback (updating a progressbar) on the UI. When I close this Activity, the OS could end the lifecycle and Destroy the Activity, calling onDestroy(), right? I simulate this, calling finish() in onPause() method. So once I restart the Activity, how to I bind to the SAME Service again? I thought that Services are Singelton, but when I'm trying to re-bind, I get another binder reference. So binder.callbackHandler.post(binder.progressHandler); still has the reference to the old binder/callback/progressHandler, not to my new one. Even the Constructor of the Service is called again!

Is there any solution to have a progressbar, getting updated by callback objects from the service (working). Closing/onDestroy() the Activity. Come back, and continue the progressbar?

My code is quite large, but recreated the 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!
                    }
        }
}

解决方案

I got it now. The solution is to explicit call startService(serviceIntent); before you bind to the Service using getApplicationContext().bindService(serviceIntent,mTestServiceConnection, Context.BIND_AUTO_CREATE);

Reason: When you start a Service with bindService(), it becomes a Bound Service an

runs only as long as another application component is bound to it.

If you start a Service with startService() it can

can run in the background indefinitely,

So if you have e.g. a progessbar on the UI, and you want it to continue updating it, you should start your Service, and bind and undbind it in onResume() / onPause(). But be carfull: Since you started the Service manually, You should also stop it manually. The simplest way to do this is call stopSelf() once the Service did it's work.

This soultion covers a proper binding from an Activity with e.g. an progresss bar to the same Service even after the activity is destroyed or after an orientation change.

这篇关于结合运行Service(后完成())/回调处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆