每个服务绑定是否需要一个ServiceConnection? [英] Is there a need to have one ServiceConnection per each Service bind?

查看:467
本文介绍了每个服务绑定是否需要一个ServiceConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绑定到Activity的多个Android Service,因此我可以监视用户的多个操作.

I have several Android Services that I want to bind to in my Activity, so I can monitor several actions from the user.

为了能够绑定每个服务,我将拥有多个服务,我的活动中是否需要几个私有的ServiceConnection,如下所示?

To be able to bind every Service, and I will have several, do I need several private ServiceConnections in my activity like the following?

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
        PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
        gpsService = gpsBinder.getService();
        photoService = photoBinder.getService();
        mGpsBound = true;
        mPhotoBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mGpsBound = false;
        mPhotoBound = false;
    }
};

还是我需要在活动"和服务"之间添加一个管理器类,以更好地使用和理解有限的服务?

Or do I need a manager class between my Activity and the Services that provides better usage and understanding of the bounded Services?

推荐答案

每个Android是否需要一个serviceConnection 服务吗?

Is there a need to have one serviceConnection for each android service?

我假设您正在询问是否可以将相同的serviceConnection重用于多种服务.每个服务连接都不需要一个,但这可能是最好的方法.我在您的代码中看到了

I assume you're asking if you can reuse the same serviceConnection for multiple services. There's no need to have one for each service connection, but this is probably the best approach. I see in your code this

        GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
        PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
        gpsService = gpsBinder.getService();
        photoService = photoBinder.getService();

这非常令人困惑...这似乎可以将一项服务强制转换为两个不同的服务!

This is very confusing... this seems like a service can be cast into two different services!!

您将意识到onServiceConnected回调是发生大多数魔术的地方,您(活动)最终可以在其中获得指向Service的指针并开始调用方法并与服务进行交互.如果要对不同的服务重用相同的serviceConnection,则需要找出IBinder对象属于哪个自定义子类,然后进行适当的转换.乌夫,太麻烦了.我建议每项服务有一个serviceConnection.

You'll realize that the onServiceConnected callback is where most of the magic happens, where you (the Activity) finally can get a pointer to your Service and start calling methods and interact with your service. If you want to reuse the same serviceConnection for different services you'd need to find out which custom subclass the IBinder object belongs to and then cast appropriately. Ufff, too much trouble. I would recommend having one serviceConnection per service.

还是我需要在我的活动和所提供的服务之间使用经理类 可以更好地使用和了解有限服务?

Or do i need a manager class between my activity and the services that provides better usage and understanding of the bounded services?

对于这个问题和第一个问题,您都可以做任何想做的事情.没有一种方法比另一种方法更好(IMHO),最好的一种方法是您能更好地理解并让您感觉更舒适.

For both this and your first question, you can do whatever you want. There's no approach better than the other (IMHO) and the best one is the one you understand better and makes you feel more comfortable.

这篇关于每个服务绑定是否需要一个ServiceConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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