2011-03-22 4 views
8

En suivant les exemples de Google pour l'utilisation d'un service, j'ai créé un certain nombre de threads comme celui-ci. Je ne peux pas utiliser IntentService, parce que je fais quelque chose qui implique d'attendre des rappels.Comment mettre fin à HandlerThreads?

Cependant, je ne sais pas comment terminer un Thread démarré de cette manière. Si je comprends bien, Threads se termine automatiquement lorsque la méthode run() renvoie. Cependant, ce type de thread n'a pas de méthode d'exécution. Mes fils fuient - ils restent en vie après stopSelf().

@Override 
public void onCreate() { 

    HandlerThread thread = new HandlerThread("ServiceStartArguments", 
      android.os.Process.THREAD_PRIORITY_BACKGROUND); 
    thread.start(); 
    HandlerThread thread2 = new HandlerThread("CallbackHandling", 
      android.os.Process.THREAD_PRIORITY_BACKGROUND); 
    thread2.start(); 

    mServiceLooper = thread.getLooper(); 
    mCallbackLooper = thread2.getLooper(); 
    mServiceHandler = new MyHandler(mServiceLooper); 
    mCallbackHandler = new Handler(mCallbackLooper); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 

    // For each start request, send a message to start a job and deliver the 
    // start ID so we know which request we're stopping when we finish the job 
    Message msg = mServiceHandler.obtainMessage(); 
    msg.arg1 = startId; 
    mServiceHandler.sendMessage(msg); 
    mMainThreadHandler=new Handler(); 
    // If we get killed, after returning from here, restart 
    return START_STICKY; 
} 

private final class MyHandler extends Handler { 
    public MyHandler(Looper looper) { 
     super(looper); 
    } 
    @Override 
    public void handleMessage(Message msg) { 
     cycle(); 


     // Stop the service using the startId, so that we don't stop 
     // the service in the middle of handling another job 
     stopSelf(msg.arg1); 
    } 
} 

protected void cycle() { 
    ... 
    mCallbackHandler.post(new Runnable(){ 
     public void run(){ 
      goAskForSomeCallbacks(); 
     } 
    }); 

    try { 
     Thread.sleep(GIVE_UP_TIME); 
    } catch (InterruptedException e){ 
     //The callback will interrupt this thread to stop it from waiting 
     Log.d(TAG,"Got early callback, stop waiting."); 
    } 
    Thread.interrupted(); //clear the interrupt 
    doStuff(); 

} 
+0

Je ne vois pas pourquoi l'utilisation de rappels vous empêcherait d'utiliser un IntentService. J'espère que "attendre des rappels" n'implique pas d'attente ici. Si oui, alors je suggère une refonte. Si ce n'est pas le cas, il n'y a pas de raison de garder HandlerThread séparé sur une base de rappel par anticipation. Même si un post-traitement lourd des données fournies par les rappels est impliqué, il pourrait être sage de faire la queue pour cela, etc. Donc, ce serait génial de comprendre l'architecture que vous avez en tête. –

Répondre

16

Essayez d'appeler la méthode quit sur les arpenteuses correspondants.

+0

@ TenFour04 publieriez-vous la solution finale? – eros

-1

Forcer la fermeture de l'application le fait mais sinon ils continuent tout seul.

1

Vous pouvez appeler la méthode quit sur vos threads de gestionnaire.

par exemple.

thread.quit(); 
thread2.quit();