2017-05-19 211 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

相關問題