2011-08-01 46 views

回答

0

剛剛得到的服務和使用的AsyncTask的背景下,以創建另一個線程...或者你也可以做

new Thrad(){ 
public void Run(){ 
//your implementation.. 
} 
} 
1

您在服務啓動一個線程你什麼啓動一個線程的方式相同。使用Java線程,定時器或異步任務。

Java線程通常開始像這樣:

private Thread yourThread; 
private NewRunnable yourRunnable; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    ... code... 
    yourThread = new Thread(yourRunnable); 
    ... code... 
} 

private final class NewRunnable extends Runnable 
{ 
    @Override 
    public void run() 
    { 
    ... Code here will be run in new thread.... 
    } 
} 
1

這樣的偉大工程。

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 

public class Servicio extends Service { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     initialize(); 
    } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    public void initialize(){ 
     Thread th = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       //Your code ...... 

      } 
     }); 
     th.start(); 
    } 

}