如何开始在Android的新线程的服务 [英] How to start service in new thread in android
问题描述
我是新来这个机器人。我使用的是服务,做一些后台的工作。所以我开始从我的活动的服务如下。
I'm new to this android. i'm using a service to do some background work. so i'm starting the service from my activity as following.
getApplicationContext().bindService(
new Intent(getApplicationContext(), MyAndroidUpnpServiceImpl.class),
serviceConnection,
Context.BIND_AUTO_CREATE
);
但问题是android的活动被阻止。直到该服务,
but the problem is android activity is blocked. untill the service,
onServiceConnected(ComponentName className, IBinder service){ ..}
被称为back.so我搜索关于此。我才知道,我要开始我的服务新的主题。所以请任何一个帮助我做这个。
is called back.so i searched regarding this. i came to know that i have to start my service in new Thread. so please any one help me in doing this.
推荐答案
要创建并启动一个新线程,从活动里,你可以说:
To create and start a new thread, from inside an activity, you can say:
Thread t = new Thread(){
public void run(){
getApplicationContext().bindService(
new Intent(getApplicationContext(), MyAndroidUpnpServiceImpl.class),
serviceConnection,
Context.BIND_AUTO_CREATE
);
}
};
t.start();
此外,缓存由bindservice返回的值,如果有的话,如果你需要它供以后使用。
Also, cache the value returned by bindservice, if any, if you require it for later use.
这篇关于如何开始在Android的新线程的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!