为什么当主要活动被关闭远程服务被破坏? [英] Why remote service is destroyed when main activity is closed?

查看:100
本文介绍了为什么当主要活动被关闭远程服务被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个机器人程序:具有UI中的主要活动,并启动的服务。服务及时回调UI活动以更新的观点。它工作正常,但:如果活动被关闭(背部),并再次启动,该服务也将重新开始(该服务播放音频文件,所以有两个重叠的声音)。 我用bindService与BIND_AUTO_CREATE标志启动并连接到服务。根据该文件,它应该创建服务只有当它不存在,但显然在启动时打开第二时间另一个实例。 我想要的是,当活动被关闭时,该服务的推移的运行,并且当活动再次打开时,它可以重新连接到服务。那可能吗?或者,我只是误会服务的使用情况如何?

尝试更多: 使用ICountService(在.aidl描述)代替CountService在bindService意图。它onDestroyed活动时关闭被称为

下面是$ C $服务创建,如果它帮助了C。

  ServiceConnection康恩=新ServiceConnection(){
    @覆盖
    公共无效onServiceConnected(组件名C,的IBinder B){
        Log.d(TK,保持通话);
        //粘合剂=(ICountService.Stub)B:
        服务= ICountService.Stub.asInterface(B);
        尝试 {
            service.setCallback(新ICountCallback.Stub(){

                @覆盖
                公共无效警报(){
                    Log.d(TK,警告!);
                }

                @覆盖
                公共无效录入(最终诠释秒){
                    handler.post(新的Runnable(){

                        @覆盖
                        公共无效的run(){
                                                            indicator.setText(toText(秒));
                        }

                    });
                }
            });
        }赶上(RemoteException的E){
            e.printStackTrace();
        }
    }

    @覆盖
    公共无效onServiceDisconnected(组件名C){
        Log.d(TK,断开连接);
    }
};

私人无效startCountService(){
    意图I =新的意图(ICountService.class.getName());
    布尔OK = context.bindService(我,康涅狄格州,Context.BIND_AUTO_CREATE);
    Log.d(TK,bindService =+ OK);
}
 

解决方案
  

根据该文件,它应该创建服务只有当它不存在,但显然在启动时打开第二时间另一个实例

bindService()如果服务没有运行,将创建服务实例。 unbindService()会破坏服务实例,如果没有其它绑定连接,没有人叫 startService()。如果该服务的破坏unbindService(),那么随后的 bindService()将创建一个新的服务实例。

恕我直言,理想情况下, unbindService()将不会立即销毁服务,但让它苟延残喘了几秒钟第一,如果有一个 bindService()后不久, unbindService()。然而,这是不是他们是如何实现的吧。

  

我要的是,当活动被关闭时,该服务的推移运行,并且当活动再次打开时,它可以重新连接到服务

您应该使用 startService() stopService(),而不是(或想像除) bindService() unbindService()

I wrote an android program that: has a main activity for UI, and it starts a service. The service timely callbacks the UI activity to update views. It works fine except: if the activity is closed (with BACK) and start again, the service will also be started again (The service plays audio file, so there are two overlapped sounds). I use bindService with BIND_AUTO_CREATE flag to start and connect to service. According to the document, it should create service only if it doesn't exist, but obviously it starts another instance when opened second time. All I want is when the activity is closed, the service goes on running, and when the activity opens again, it can reconnect to the service. Is that possible? Or I just misunderstand the usage of service?

Tried more: Use ICountService (described in .aidl) instead of CountService in bindService Intent. It's onDestroyed is called when the activity is closed.

Below is code of service creating if it helps.

    ServiceConnection conn = new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName c, IBinder b) {
        Log.d("TK","Connected");
        //binder = (ICountService.Stub) b;
        service = ICountService.Stub.asInterface(b);
        try {
            service.setCallback(new ICountCallback.Stub(){

                @Override
                public void alert() {
                    Log.d("TK","alert!");
                }

                @Override
                public void updateTime(final int sec) {
                    handler.post(new Runnable(){

                        @Override
                        public void run() {
                                                            indicator.setText(toText(sec));
                        }

                    });
                }               
            });
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName c) {
        Log.d("TK","Disconnected");
    }           
};

private void startCountService(){
    Intent i = new Intent(ICountService.class.getName());
    boolean ok = context.bindService(i, conn, Context.BIND_AUTO_CREATE);
    Log.d("TK", "bindService="+ok);
}

解决方案

According to the document, it should create service only if it doesn't exist, but obviously it starts another instance when opened second time.

bindService() will create the service instance if the service is not running. unbindService() will destroy the service instance if there are no other bound connections and nobody called startService(). If the service is destroyed on unbindService(), then a subsequent bindService() will create a new service instance.

IMHO, ideally, unbindService() would not immediately destroy the service, but let it linger for a few seconds first, in case there is a bindService() shortly after the unbindService(). However, that is not how they implemented it.

All I want is when the activity is closed, the service goes on running, and when the activity opens again, it can reconnect to the service.

You should be using startService() and stopService() instead of (or conceivably in addition to) bindService() and unbindService().

这篇关于为什么当主要活动被关闭远程服务被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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