Java 如何在 Android 中绑定此服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind this service in Android?
提问by TIMEX
This is the code in my Activity
. Initiate an Intent, then a Connection, right?
这是我的Activity
. 启动一个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?
但我的问题是... 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 onServiceConnectedand onServiceDisconnected?
有人能告诉我我在onServiceConnected和onServiceDisconnected 中放入了什么代码吗?
I just want a basic connection so that my Activity
and Service
can talk to each other.
我只想要一个基本的连接,以便我Activity
和我Service
可以互相交谈。
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/
回答by Chris.D
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 提供的服务示例,那么您的服务将泄漏内存,请参阅有关如何正确执行此操作的优秀文章(并投票支持相关的 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
回答by Nikhil_Katre
Binding to a service from an Activity should be avoided as it causes probems when the Activity Configurations change (e.g. 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
Communicate with Activity from Service (LocalService) - Android Best Practices
应该避免从 Activity 绑定到服务,因为它会在 Activity 配置更改时导致探测(例如,如果设备旋转,则 Activity 将从头开始再次创建,并且必须重新创建绑定)。
请参考来自 Commonsware 的评论,关于 stackoverflow
Communicate with Activity from Service (LocalService) - Android Best Practices
回答by KnowIT
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. Have a look at this Bound Service Example.
这里 mBoundService 是你绑定服务的对象。看看这个绑定服务示例。