2014-12-08 79 views
0

中的线程(进度条)我已经使用进度条和线程作出了倒数计时器,现在我想在用户单击按钮时同时停止进度。我尝试了thread.stop( ),但它说有。没有这样的方法我都试过interruot方法太没有运气,所以可以任意好友,请通过查看我的code.My代码帮助我是如下: 代码如何停止android

package com.amar.lyricalgenius; 

import com.amar.lyricalgenius.LoadingActivity.MyCountDownTimer; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SinglePlayerOption extends Activity implements OnClickListener { 

    // visible gone 
    private int progressStatus = 0; 
    private Handler handler = new Handler(); 
    Intent i; 
    TextView text_player_second; 
    ImageView vs_word; 
    ImageView player_second_pic, player_second_box; 
    ImageButton red_point1; 
    TextView text_number_pt1; 
    TextView text_number1; 
    ProgressBar pg_loading; 
    private CountDownTimer countDownTimer; 
    TextView timer_text; 
    private final long startTime = 8 * 1000; 
    private final long interval = 1 * 1000; 
    Button opt_1, opt_2, opt_3, opt_4; 
    Thread splashThread; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.single_player_option); 

     init(); 

    } 

    private void init() { 
     // TODO Auto-generated method stub 
     text_player_second = (TextView) findViewById(R.id.text_player_second); 
     vs_word = (ImageView) findViewById(R.id.vs_word); 
     player_second_pic = (ImageView) findViewById(R.id.player_second_pic); 
     player_second_box = (ImageView) findViewById(R.id.player_second_box); 

     red_point1 = (ImageButton) findViewById(R.id.red_point1); 
     text_number_pt1 = (TextView) findViewById(R.id.text_number_pt1); 
     text_number1 = (TextView) findViewById(R.id.text_number1); 

     opt_1 = (Button) findViewById(R.id.option_1); 
     opt_2 = (Button) findViewById(R.id.option_2); 
     opt_3 = (Button) findViewById(R.id.option_3); 
     opt_4 = (Button) findViewById(R.id.option_4); 

     opt_1.setOnClickListener(this); 
     opt_2.setOnClickListener(this); 
     opt_3.setOnClickListener(this); 
     opt_4.setOnClickListener(this); 
     text_player_second.setVisibility(View.GONE); 
     vs_word.setVisibility(View.GONE); 
     player_second_pic.setVisibility(View.GONE); 
     player_second_box.setVisibility(View.GONE); 
     red_point1.setVisibility(View.GONE); 
     text_number_pt1.setVisibility(View.GONE); 
     text_number1.setVisibility(View.GONE); 
     countDownTimer = new MyCountDownTimer(startTime, interval); 
     timer_text.setText(timer_text.getText() 
       + String.valueOf(startTime/1000)); 

     countDownTimer.start(); 

     new Thread(new Runnable() { 
      public void run() { 
       while (progressStatus < 100) { 
        progressStatus += 1; 
        // Update the progress bar and display the 

        // current value in the text view 
        handler.post(new Runnable() { 
         public void run() { 
          pg_loading.setProgress(progressStatus); 
         } 
        }); 
        try { 
         // Sleep for 200 milliseconds. 

         // Just to display the progress slowly 
         Thread.sleep(62); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 

     splashThread = new Thread() { 
      public void run() { 
       try { 
        sleep(6000); 
        // Utils.systemUpgrade(SplashActivity.this); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       Intent intent = null; 
       intent = new Intent(SinglePlayerOption.this, 
         NoResponseActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(intent); 

       finish(); 

      } 
     }; 
     splashThread.start(); 

    } 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent i; 
     switch (v.getId()) { 
     case R.id.option_1: 
      splashThread.stop(); 
      countDownTimer.onFinish(); 
      Toast.makeText(SinglePlayerOption.this, 
        timer_text.getText().toString(), 1).show(); 
      i = new Intent(SinglePlayerOption.this, 
        DialogLeaderboardActivity.class); 
      startActivity(i); 
      break; 
     } 
    } 

    public class MyCountDownTimer extends CountDownTimer { 
     public MyCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     @Override 
     public void onFinish() { 
      timer_text.setText("Time's up!"); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      timer_text.setText("" + millisUntilFinished/1000); 
     } 
    } 
} 

回答

1
Thread th = new Thread(new Runnable() { 
      public void run() { .... 

th.start();//starts 

th.interrupt();//this stops. 

and use

while (progressStatus < 100 && (!Thread.currentThread().isInterrupted())){.... 

代替

while (progressStatus < 100) {.... 
0

现在我想停止在当用户点击一个 的button.I同一时间进度已经尝试使用Thread.stop()

Thread.stop()已被弃用你不应该使用它。要理解的基本概念是线程终止在执行其run方法的最后一行代码时执行。在你的“ProgressBar Thread *”的情况下,当用户按下按钮时,你必须设置你的while循环(progressStatus = 100)的退出条件以使其终止

+0

在哪里可以放这个条件, code.Please – jigar 2014-12-08 08:57:20

+0

没有看到整个代码是不可能的,但如果你保留'progressStatus',作为你类的'volatile'成员,当用户按下按钮时,你只需要设置'progressStatus = 100;' – Blackbelt 2014-12-08 09:00:13

+0

我有加我整个activity.java ,,请帮助我。 – jigar 2014-12-08 09:29:20