停止线程里面有服务 [英] Stopping a thread inside a service

查看:91
本文介绍了停止线程里面有服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程服务中,我希望能够停止线程,当我$在我的主要活动类p $ PSS的 buttonStop

I have a thread inside a service and I would like to be able to stop the thread when I press the buttonStop on my main activity class.

在我的主要活动类,我有:

In my main activity class I have:

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; 
    }           
  }
}

和我的服务类,我有:

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

what is the best way to stop the mythread?

还有就是,我已经通过 stopService(新意图(这一点,MyService.class))停止服务的方式; 正确

Also is the way that I have stopped the service by stopService(new Intent(this, MyService.class)); correct?

推荐答案

您不能阻止一个线程,有一个正在运行的不可阻挡的循环像这样

You can't stop a thread that has a running unstoppable loop like this

while(true)
{

}

要停止该线程,声明布尔变量,并用它在while循环条件。

To stop that thread, declare a boolean variable and use it in while-loop condition.

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();

}

这篇关于停止线程里面有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆