简体   繁体   English

如何在Android中绑定此服务?

[英]How do I bind this service in Android?

This is the code in my Activity . 这是我的Activity的代码。 Initiate an Intent , then a Connection , right? 启动一个Intent ,然后一个Connection ,对吧?

hello_service = new Intent(this, HelloService.class);
hello_service_conn = new HelloServiceConnection();
bindService( hello_service, hello_service_conn, Context.BIND_AUTO_CREATE);

But my question is...what goes inside the Connection? 但我的问题是......连接内部是什么?

   class HelloServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName className,IBinder boundService ) {

        }
        public void onServiceDisconnected(ComponentName className) {

        }
    };

Can someone tell me what code I put into onServiceConnected and onServiceDisconnected ? 有人能告诉我在ServiceConnectedonServiceDisconnected上添加了哪些代码?

I just want a basic connection so that my Activity and Service can talk to each other. 我只想要一个基本连接,以便我的ActivityService可以相互通信。

Edit: I found a good tutorial, and I can actually close this question, unless someone wants to answer. 编辑:我找到了一个很好的教程,我实际上可以关闭这个问题,除非有人想回答。 http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/ http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

I would like to point out that if you follow the service examples provided by google then your service will leak memory, see this chaps excellent post on how to do it properly (and vote for the related Google bug) 我想指出,如果您按照谷歌提供的服务示例,那么您的服务将泄漏内存,请参阅这篇章节如何正确地做到这一点(并投票支持相关的Google错误)

http://www.ozdroid.com/#!BLOG/2010/12/19/How_to_make_a_local_Service_and_bind_to_it_in_Android http://www.ozdroid.com/#!BLOG/2010/12/19/How_to_make_a_local_Service_and_bind_to_it_in_Android

Binding to a service from an Activity should be avoided as it causes probems when the Activity Configurations change (eg if the device is rotated the activity would get created again from scratch and the binding would have to be re-created). 应该避免从活动绑定到服务,因为它会在活动配置发生变化时导致探测(例如,如果设备被轮换,活动将从头再次创建,并且必须重新创建绑定)。
Please refer to the comment from Commonsware on the following post on stackoverflow 有关stackoverflow的以下帖子,请参阅Commonsware的评论
Communicate with Activity from Service (LocalService) - Android Best Practices 与服务中的活动(LocalService)进行通信 - Android最佳实践

To connect a service to an activity, all you need to write in a ServiceConnection implementation is : 要将服务连接到活动,您需要在ServiceConnection实现中编写的所有内容是:

@Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder myBinder = (MyBinder) service;
mBoundService = myBinder.getService();
mServiceBound = true;
}

Here mBoundService is an object of your bound service. 这里mBoundService是绑定服务的对象。 Have a look at this Bound Service Example . 看看这个绑定服务示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM