线程在销毁Activity后仍然在运行

时间:2012-07-18 07:49:10

标签: android multithreading

我通过调用finish()来销毁我的活动。但是活动中的线程在销毁活动后仍在运行,导致应用程序崩溃。 我怎么处理这个?为什么线程在销毁活动后运行? 请提供一些帮助吗?

class DBThread extends Thread {

    @Override
    public void run() {
        while (finish) {
            Cursor cursor = null;

            Cursor mCursor = lrDB.selectUserDetails(mUser_Id);
            if (mCursor.getCount() > 0) {
                mCursor.moveToNext();
                String Name = mCursor .getString(0);
                String userName = mCursor .getInt(1);
                String password = mCursor .getString(3);

            }
            mCursor.close();

        }
    }
}

6 个答案:

答案 0 :(得分:2)

首先纠正你的主题:

class DBThread extends Thread { 
    @Override 
    public void run() { 
        while (!isInterrupted()) {
            Cursor mCursor = lrDB.selectUserDetails(mUser_Id); 
            if (mCursor.getCount() > 0) { 
                mCursor.moveToNext(); 
                String Name = mCursor .getString(0); 
                String userName = mCursor .getInt(1); 
                String password = mCursor .getString(3); 

            } 
            mCursor.close(); 
        } 
    } 
} 

其次,在活动中优雅地关闭线程:

@Override
public void onDestroy() {
    if (yourThread!=null) {
        yourThread.interrupt(); // request to terminate thread in regular way
        yourThread.join(500); // wait until thread ends or timeout after 0.5 second
        if (yourThread.isAlive()) {
            // this is needed only when something is wrong with thread, for example hangs in ininitive loop or waits to long for lock to be released by other thread.
            Log.e(TAG, "Serious problem with thread!");
            yourThread.stop();
        }
    }
    super.onDrestroy();
}

请注意,Thread.destroy()已弃用,不起作用。

答案 1 :(得分:1)

Thread在退出run方法时停止运行。所以,如果你有像

这样的模式
public void run() {
  while(condition) {
    // exec
  }
}

记住将condition设置为false,以便让线程完成执行

class DBThread extends Thread {

private boolean finish = true;    

public void stopThread() {
   finish = false;
}

@Override
public void run() {
    while (finish) {
        Cursor cursor = null;

        Cursor mCursor = lrDB.selectUserDetails(mUser_Id);
        if (mCursor.getCount() > 0) {
            mCursor.moveToNext();
            String Name = mCursor .getString(0);
            String userName = mCursor .getInt(1);
            String password = mCursor .getString(3);

        }
        mCursor.close();

    }
 }
}

onDrestroy()来电yourThreadInstance.stopThread();

内部

答案 2 :(得分:1)

添加以下代码:

@Override
    protected void onDestroy() {
        thread.stop();
        thread.destroy();
        super.onDestroy();
    }

答案 3 :(得分:0)

您可以在onStop或onDestroy方法中处理这种情况。在onDestroy方法中断线程。

答案 4 :(得分:0)

@Override     
protected void onStop() {
         thread.stop();
           super.onDestroy();
 }


 @Override     
protected void onPouse() {
         thread.stop();
        super.onDestroy();
}

答案 5 :(得分:0)

您可以调用正在运行的线程的中断函数,并在线程上验证您是否使用isInterrupted()函数中断并继续。