我遇到了一个小问题,我不明白。我有一个简单的进度条,但Thread.interrupt不会停止该线程。我必须把它变成一个全局变量。我想知道是否有人能够阻止这个问题。无法停止Android上的进度条使用Thread.interrupt
我想这个线程,但我没有工作:
How to stop a thread(progressbar) in android
这里是与黑客
// Start lengthy operation in a background thread
calcThread = new Thread
(
new Runnable()
{
public void run()
{
Thread current = Thread.currentThread();
//while (!current.isInterrupted()) // this does not
while (threadLoop) // this hack works
{
doWork();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
Log.d(TAG, "out of thread loop");
}
}
);
calcThread.start();
现在
,我试图阻止线程
public void onClickAbout(View view)
{
if (view.getId() == R.id.buttonAbout)
{
Log.d(TAG, "onButtonPressed");
calcThread.interrupt(); // This does not work
threadLoop = false; // this works.
}
}
代码
为什么我必须破解全球?换句话说,为什么Thread.interrupt不会停止线程。
thx!
为什么不使用asyncTask? – k0sh