2013-02-05 22 views
6

Bir hizmetin içinde bir iş parçacığım var ve ana etkinlik sınıfımdaki buttonStop düğmesine bastığımda iş parçacığı durdurmak istiyorum. benim ana faaliyet sınıftaBir iş parçacığının içinde bir iş parçacığının durdurulması

Ben:

public class MainActivity extends Activity implements OnClickListener { 
    ... 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    buttonStart = (Button) findViewById(R.id.buttonStart); 
    buttonStop = (Button) findViewById(R.id.buttonStop); 

    buttonStart.setOnClickListener(this); 
    buttonStop.setOnClickListener(this); 
    } 

    public void onClick(View src) { 
    switch (src.getId()) { 
    case R.id.buttonStart: 
     startService(new Intent(this, MyService.class)); 
     break; 
    case R.id.buttonStop: 
     stopService(new Intent(this, MyService.class)); 
     break; 
    }   
    } 
} 

Ve hizmet sınıfında

Ben:

public class MyService extends Service { 
    ... 
    @Override 
    public IBinder onBind(Intent intent) { 
    return null; 
    } 

@Override 
public void onCreate() { 
    int icon = R.drawable.myicon; 
    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 
    Notification notification = new Notification(icon, tickerText, when); 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(this, "notification title", "notification message", pendingIntent);  
    startForeground(ONGOING_NOTIFICATION, notification); 
      ... 
} 

@Override 
public void onStart(Intent intent, int startid) { 
    Thread mythread= new Thread() { 
    @Override 
    public void run() { 
    while(true) { 
       MY CODE TO RUN; 
      } 
    } 
    } 
}; 
mythread.start(); 
} 

}

mythread durdurmanın en iyi yolu nedir?

Hizmetin stopService(new Intent(this, MyService.class)); tarafından durdurulma şekli de doğru mu?

cevap

9

O parçacığı durdurmak için bu

while(true) 
{ 

} 

gibi çalışan durdurulamaz döngüsü var bir iş parçacığı duramayız, bir boolean değişken bildirmek ve süre döngü durumda kullanın.

public class MyService extends Service { 
     ... 
     private Thread mythread; 
     private boolean running; 



    @Override 
    public void onDestroy() 
    { 
     running = false; 
     super.onDestroy(); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 

     running = true; 
     mythread = new Thread() { 
     @Override 
     public void run() { 
     while(running) { 
        MY CODE TO RUN; 
       } 
     } 
     }; 
    }; 
    mythread.start(); 

} 
+0

'buttonStop' düğmesine bastığımda ve servive geçirdiğimde boole değişkeni nasıl değiştirebilirim? – TJ1

+0

Bunu yapmak zorunda değilsiniz, 'stopService()', 'onDestroy()' ın hizmetini çağırmanız gerekecek, o zaman boolean yanlış ayarlanacaktır. –

+0

Aslında, şu anda kullandığım kodu durdurabilmem gerekir. ("KODUNU ÇALIŞTIR") koşuyor, bu yüzden 'buttonStop''a bastığımda 'çalışmayı' değiştirebilmem gerekiyor. – TJ1

-2

Sen stop hizmet için OnDestroy() yöntemini çağırın.

İlgili konular