0

服务和活动可以同时在同一个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 进程)的同一进程中执行?

4

2 回答 2

1

默认情况下,每个应用程序都在自己的进程中运行,并且应用程序的所有组件都在该进程中运行。

服务和活动可以同时在同一个linux进程中运行吗?

上面的陈述已经回答了这个问题,下面是 Services 文档再次所说的:

Service 对象本身并不意味着它在自己的进程中运行;除非另有说明,否则它在与其所属的应用程序相同的进程中运行。

如需进一步阅读,请考虑以下链接:http: //developer.android.com/guide/components/processes-and-threads.html

于 2013-10-07T07:09:28.823 回答
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

于 2013-10-07T07:12:24.253 回答