2012-07-18 52 views
0

我已经通过调用finish()摧毁了我的活动,但活动中的线程仍然运行后摧毁活动,并导致应用程序崩溃.. 我该如何处理?线程在破坏活动后运行? 请提供一些帮助?线程仍然runnng后摧毁活动

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(); 

     } 
    } 
} 
+0

您可能需要使用'ExecutorService'代替'Thread',这是更好的。它有一个'shutdown'和awaitTermination方法 – 2012-07-18 07:56:13

+0

线程继续运行,因为它是它自己的一个小进程 – 2012-07-18 08:02:07

回答

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(); 
     } 
    } 
} 

其次,关闭线程gracefuly做这样的事情,在活动:

@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()已被弃用,将无法正常工作。

+0

Thread.stop()也被弃用了。所以避免了这个循环,它已经工作了。非常感谢你的帮助。 – user987362 2012-07-19 05:10:56

+0

Thread.stop()yes它已被弃用它可以制动虚拟机的状态,所以它应该只用作最后的手段,这是我的代码做了什么,这就是为什么我的代码记录一个错误。 – 2012-07-19 08:31:52

0

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

+0

没有为我工作。即使应用程序崩溃 – user987362 2012-07-18 08:25:20

1

Thread停止,当它退出run方法运行。所以,如果你有一个像

public void run() { 
    while(condition) { 
    // exec 
    } 
} 

remeber模式设置conditionfalse,为了让线程完成其执行onDrestroy()呼叫yourThreadInstance.stopThread();

+0

我已经做到了,但线程运行..如果activity在线程中间被销毁执行会做什么? – user987362 2012-07-18 08:23:29

+0

可以提供运行中的退出点。只是在离开活动时重新设置退出条件。如果它不工作,意味着该问题与别的 – Blackbelt 2012-07-18 08:25:57

+0

仍然存在问题:( – user987362 2012-07-18 08:53:14

1

添加以下里面

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(); 

    } 
} 
} 

的代码:

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


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

可以调用运行的线程的中断功能和线程上验证,如果你使用的isInterrupted()函数,并相应地继续中断。