我正在寻找一种方法来停止/杀死一个线程,我看到Thread.stop()已被弃用。所以我开始寻找另一种解决方案,看到多个职位表明这一点:寻找一种方法来停止/杀死一个线程
thread.interrupt();
thread = null;
但是,就不会停止我的主题,我的主题是这样的:
public static Thread loadingSpinnerTimer (final Context context, final ImageView imageView){
final Handler mHandler = new Handler();
thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (!isPaused) {
try {
Thread.sleep(100);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
Log.e("ThreadMessage","HasRun");
if (ticker == 12){
ticker = 0;
}
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.spinner_160x160);
Bitmap img = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.ARGB_8888); //ARGB_8888 transparrent?
Canvas canvas = new Canvas(img);
int offset = myBitmap.getWidth()*ticker;
Rect srcrectangle = new Rect(0, offset, myBitmap.getWidth(), myBitmap.getWidth()+offset);
Rect dstrectangle = new Rect(0, 0, myBitmap.getWidth(), myBitmap.getWidth());
canvas.drawBitmap(myBitmap,srcrectangle,dstrectangle,null);
img.setHeight(myBitmap.getWidth());
imageView.setImageDrawable(new BitmapDrawable(context.getResources(), img));
ticker++;
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
});
thread.start();
return thread;
}
任何人都得到一个解决方案,我可以用来停止/杀死我的线程?任何帮助深表感谢。
检查[复位标志oracle docs](http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html) – TheLostMind 2014-10-10 15:08:43
看到这个[link](http://stackoverflow.com/a/20528033/4101719) – coderhyme 2014-10-10 15:22:00