服务和活动可以同时在同一个linux进程中运行吗?
private class Test extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
// Computational logic
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
线程是在新的linux进程中执行还是在调用此服务的同一进程(UI进程)中执行?。
答案 0 :(得分:1)
默认情况下,每个应用程序都在自己的进程中运行,并且应用程序的所有组件都在该进程中运行。
服务和活动可以同时在同一个linux进程中运行吗?
以上陈述已经回答了这个问题,这是服务docs再一次说的话:
Service对象本身并不意味着它自己运行 处理;除非另有说明,否则它在与...相同的过程中运行 它所属的应用程序。
如需进一步阅读,请考虑以下链接:http://developer.android.com/guide/components/processes-and-threads.html
答案 1 :(得分:1)
来自文档:http://developer.android.com/reference/android/app/Service.html
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
除非你在清单中提到,否则服务将在同一个线程中运行:见:http://developer.android.com/guide/topics/manifest/service-element.html