I'm looking for a way to stop/kill a Thread, i saw that Thread.stop() is deprecated. So i started searching for another solution and saw multiple posts suggesting this:
thread.interrupt();
thread = null;
But that just won't stop my Thread, my Thread looks like this:
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;
}
Anyone get a solution i can use to stop/kill my Thread? Any help is much appreciated.