2017-05-19 210 views
-5

我有点困惑。我有包含可运行类的后台服务。在服务体中,我调用了使用我的可运行对象(在onCreate方法和onStartCommand中)创建新线程的方法。在启动新线程后,我调用了与网络一起工作的runnable类的一些方法,并且我在主线程中得到了Exception。主线程异常

我的问题是:如果我从我的服务调用独立线程的方法是在主服务线程上还是在第二个线程上执行?

public class myService extends Service { 

public class myThread implements Runnable, LogListener, .... { 
//networking methods herer 
} 
private void handleServiceStart{ 
    if (!serviceStarted) { 
     Thread thread = new Thread(myThread); 
     thread.start();  
    } 
    thread.methodWithNetowrking(); 
    ... 
} 
public void onStart() { 
    this.handleServiceStart(); 
} 
public int onStartCommand() { 
    this.handleServiceStart();  
} 

回答

0

的Android服务运行在主线程,因此任何线程ü创造形成的,这将是主线程的子线程和u不能在主线程中执行任何网络操作。您需要使用asynctask

相关问题