2016-12-04 93 views
1

我有下面的代码,其显示3 ... 2 ... 1启动一个线程:如何暂停/恢复或停止一个线程和显示弹出

... 
final Handler handler = new Handler(); 
final TextView textView = (TextView) findViewById(R.id.textView2); 
final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3); 
final Runnable counter = new Runnable() { 
    @Override 
    public void run() { 
     textView.setText(Integer.toString(n.get())); 
     if(n.getAndDecrement() >= 1) 
      handler.postDelayed(this, 1000); 
     else { 
      textView.setVisibility(View.GONE); 
      tv.post(new Roller(900)); //tv is a textView 
     } 
    } 
}; 
handler.postDelayed(counter, 1000); 
... 
ImageButton ibStop; 
ibStop.setOnClickListener(...() { 
    //stop the thread 
    //display a popup 
}); 

ImageButton ibPause; 
ibPause.setOnClickListener(...() { 
    //pause the thread 
    //display a popup 
}); 

ImageButton ibPlay; 
ibPlay.setOnClickListener(...() { 
    //display the `handler` above with the 3...2...1... 
    //resume the thread 
}); 

可运行:

private class Roller implements Runnable 
{ 
    private long delayMillis; 
    private Boolean stop = false; 

    public Roller(long delayMillis) 
    { 
     this.delayMillis = delayMillis; 
    } 

    @Override 
    public void run() 
    { 
     int min = 0; 
     int max = 1; 
     int n = rand.nextInt(max - min + 1) + min; 
     String roll = String.valueOf(n); 
     tv.setText("Random number is " + roll); //tv is a textview 

     if (roll.equals("0")) { 
      inType = 0; 
      ibRed.setImageResource(R.drawable.red_selector); 
      ibGreen.setImageResource(R.drawable.green_dark); 
     } 
     if (roll.equals("1")) { 
      inType = 1; 
      ibRed.setImageResource(R.drawable.red_dark); 
      ibGreen.setImageResource(R.drawable.green_selector); 
     } 

     tv.postDelayed(this, delayMillis); 
    } 
} 

请帮助我停止和暂停/恢复线程。

+0

如果要暂停或继续运行,必须放置一个标记变量并将其更改。 – Alon

回答

3

正如@Alon所示,您可能需要更改标志变量才能暂停并重新启动可运行。特别是你可以尝试在处理程序中使用android的消息系统。不知道这是“理想的方法”,但它为我工作在过去的:)

boolean mRunning; int AYE_AYE_CAPTAIN=4; int MESSAGE_PAUSE=3; 
    int MESSAGE_RESUME=2; 
    Runnable roller = new Runnable() { 
      @Override 
      public void run() { 
       while(mRunning) { 
        try { 
         // Do your stuff here. 
         /* In case you want to communicate with handler */ 
         Message msg = new Message(); 
         msg.what=AYE_AYE_CAPTAIN; 
         handler.sendMessage(msg); 
        } catch (InterruptedException e) { 
         // Not good!!! 
        } 
       } 
      } 
     }, timeToRun,this); 

     mRunning=true; 
     roller.start(); 

以及处理器来管理线程运行的状态。

/** Handler to keep track of and manage thread runnable state */ 
    handler = new Handler(Looper.getMainLooper()){ 

      @Override 
      public void handleMessage(Message msg) { 
       super.handleMessage(msg); 

       switch (msg.what) { 
        case MESSAGE_PAUSE: 
         // Take an action 
         mRunning=false; 
         break; 
        case MESSAGE_RESUME; 
         mRunning = true; 
         // Restart Runnable here. 
         roller.start(); 
         break; 
        default: 
         break; 
       } 
      } 
     }; 

然后通过消息执行暂停或恢复选项。

public void main sendLifeCycleMessage(int MESSAGE) { 
    Message awesomeMessage = new Message(); 

    awesomeMessage.what = MESSAGE; // 
    roller.sendMessage(awesomeMessage); 
    /* alternatively if your calling from a service: 
    mService.getHandler().sendMessage(awesomeMessage); 
    */ 
+0

他们在活动中如何加入? – Si8

+0

我不确定我是否理解您的问题 – Rob

+0

如何在我的代码中格式化您的代码? – Si8