2012-04-23 97 views
0

我遇到了一個小問題,中斷。在我的android登錄活動中,我開始一個線程(它定義了我的會話的持續時間)。我想要的是,當我在一個不同的活動/類,我按下注銷按鈕,我應該停止線程這做倒計時,並能夠再次登錄。通過ID中斷線程

我知道只有這樣才能中斷thereads是由

Thread.currentThread().interrupt(); 

,但我不希望中斷當前線程,我想它的id,其即時通訊能夠通過的getId獲得中斷線程() 。

這是我的活動類代碼:

import java.security.GeneralSecurityException; 
import java.security.MessageDigest; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.AndroidApp.R; 
import com.AndroidApp.domain.Utente; 
import com.AndroidApp.pagine.MenuPagina; 


public class LoginActivity extends Activity { 

    final String TAG = "LogIN"; 
    ArrayList<HashMap<String, String>> mylist; 

    private Button bLogin, bExit; 
    private EditText utente, passwd; 
    private MediaPlayer mpButtonClick = null; 
    private SharedPreferences mPreferences; 
    public Thread sessionTimer; 
    public long tId = -1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); 

     SharedPreferences.Editor editor = mPreferences.edit(); 
     editor.clear(); 
     editor.commit(); 

     String nome = mPreferences.getString("nome", "Nessuno"); 
     setTitle("Sessione di : " + nome); 
     Log.w("TotThreads", Integer.toString(Thread.activeCount())); 
     if (MenuPagina.reset){ 
      Log.w("LogIn", "trying to interrupt"); 


      //this is where im trying to interrypt the thread 



      MenuPagina.reset = false; 
     } 

     if (!checkLoginInfo()) { 

      mpButtonClick = MediaPlayer.create(this, R.raw.button); 

      bLogin = (Button)findViewById(R.id.bLogin); 
      bLogin.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View view) { 

        mpButtonClick.start(); 

        Log.v(TAG, "Trying to Login"); 
        utente = (EditText)findViewById(R.id.etUtente); 
        passwd = (EditText)findViewById(R.id.etPassword); 

        String username = utente.getText().toString(); 
        username = ("[email protected]"); 
        String password = md5(passwd.getText().toString()); 
        Log.v(TAG, password); 

        XMLFunctionsLogin.getInstance().setNewURL("u=" + username + "&p=" + password); 
        String xml = XMLFunctionsLogin.getXML(); 
        Document doc = XMLFunctionsLogin.xmlFromString(xml); 
        int status = XMLFunctionsLogin.errStatus(doc); 

        Log.v("status", Integer.toString(status)); 
        if ((status == 0)) { 
         NodeList nodes = doc.getElementsByTagName("login"); 
         Element e = (Element) nodes.item(0); 
         Utente utente = new Utente(); 
         utente.setIdUtente(XMLFunctionsLogin.getValue(e, "idUtente")); 
         utente.setNome(XMLFunctionsLogin.getValue(e, "nome")); 
         utente.setCognome(XMLFunctionsLogin.getValue(e, "cognome")); 
         Log.v("utente", utente.getCognome().toString()); 

         Log.v(TAG, "5"); 

         List<NameValuePair> nvps = new ArrayList<NameValuePair>(2); 
         nvps.add(new BasicNameValuePair("utente", username)); 
         nvps.add(new BasicNameValuePair("password", password)); 
         Log.v(TAG, nvps.get(0).toString()); 
         Log.v(TAG, nvps.get(1).toString()); 

         // Store the username and password in SharedPreferences after the successful login 
         SharedPreferences.Editor editor = mPreferences.edit(); 
         editor.putString("userName", username); 
         editor.putString("password", password); 
         editor.putString("idUtente", utente.getIdUtente()); 
         editor.putString("nome", utente.getNome()); 
         editor.putString("cognome", utente.getCognome()); 
         editor.commit(); 


         Log.v(TAG, "Successo"); 

         sessionTimer = new Thread() { 
          @Override 
          public void run() { 
           long tId = Thread.currentThread().getId(); 
           Log.w("TTthread Id", Long.toString(tId)); 
           for (int i = 30; i >= 0; i -= 1) { 
            if (i == 0) { 
             System.out.print("timer finito"); 
             Log.i("Timer", "timer finito"); 
             LoginActivity.this.runOnUiThread(new Runnable() { 
              public void run() { 
               Toast.makeText(LoginActivity.this, "ti si è scaduta la sessione", Toast.LENGTH_LONG).show(); 
              } 
             }); 
             Intent intent = new Intent(getApplicationContext(), LoginActivity.class); 
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
             startActivity(intent); 
             //System.exit(0); 
            } try { 
             Thread.sleep(1000); 
             Log.i("Timer", Integer.toString(i)); 
            } catch (InterruptedException e) { 
             e.printStackTrace(); 
            } finally { 
             finish(); 
            } 
           } 
          } 

         }; 
         sessionTimer.start(); 


         Log.v(TAG, "Successo2"); 


         /* 
         Counter timer = new Counter(); 
         timer.start(); 

         */ 

         Toast.makeText(LoginActivity.this, "LogIn con successo", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent(LoginActivity.this, MenuPagina.class); 
         startActivity(intent); 

        } else { 
         final String errorMessage = XMLFunctionsLogin.errStatusDesc(doc); 
         Log.v("fallimento", errorMessage); 
         LoginActivity.this.runOnUiThread(new Runnable() { 
          public void run() { 
           Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_LONG).show(); 
          } 
         }); 

         Intent intent = getIntent(); 
         finish(); 
         startActivity(intent); 
        } 
       } 

      }); 

      bExit = (Button)findViewById(R.id.bExit); 
      bExit.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View view) { 
        mpButtonClick.start(); 
        finish(); 
       } 
      }); 
     } 
    } 

    //Checking whether the username and password has stored already or not 
    private final boolean checkLoginInfo() { 
      boolean username_set = mPreferences.contains("UserName"); 
      boolean password_set = mPreferences.contains("PassWord"); 
      if (username_set || password_set) { 
       return true; 
      } 
      return false; 
    } 

    //md5 for crypting and hash 
    private static String md5(String data) { 
     byte[] bdata = new byte[data.length()]; 
     int i; 
     byte[] hash; 

     for (i = 0; i < data.length(); i++) 
      bdata[i] = (byte) (data.charAt(i) & 0xff); 

     try { 
      MessageDigest md5er = MessageDigest.getInstance("MD5"); 
      hash = md5er.digest(bdata); 
     } catch (GeneralSecurityException e) { 
      throw new RuntimeException(e); 
     } 

     StringBuffer r = new StringBuffer(32); 
     for (i = 0; i < hash.length; i++) { 
      String x = Integer.toHexString(hash[i] & 0xff); 
      if (x.length() < 2) 
       r.append("0"); 
      r.append(x); 
     } 
     return r.toString(); 
    } 
} 

任何幫助,將不勝感激。 謝謝!

回答

2

當你啓動一個線程,你應該保存Thread對象,然後調用中斷該Thread對象:

Thread thread = new Thread(yourRunnable); 
thread.start(); 
... 
thread.interrupt(); 

所以你剛纔添加的代碼,你會怎麼做:

sessionTimer.interrupt(); 

此外,請注意,中斷線程只會導致sleep()wait()調用拋出InterruptedException或設置線程中斷標誌。它會而不是停止執行線程。這意味着你線程應使用如下代碼:

// loop until we are interrupted 
while (!Thread.currentThread().isInterrupted()) { 
    ... 
    // if you use sleep or wait it should do: 
    try { 
     Thread.sleep(someMillis); 
    } catch (InterruptedException e) { 
     // if you are not quitting then you need to reset the interrupted flag 
     Thread.currentThread.interrupt(); 
     // otherwise it is typical to quit the thread if interrupted 
     return; 
    } 
} 

所以你剛剛發佈,當你正在做你的睡眠,你應該退出run()方法,它將停止線程的代碼。

   try { 
        Thread.sleep(1000); 
        Log.i("Timer", Integer.toString(i)); 
       } catch (InterruptedException e) { 
        // quit because we were interrupted 
        return; 
       } finally { 
        finish(); 
       } 

此外,請記住,如果一個單獨的線程上sessionTimer調用interrupt(),那麼它應該被標記爲volatile

+0

那麼我該如何有效地停止線程。因爲我的問題是,當我註銷我的計時器是stil做倒計數,如果我重新登錄然後我有2個線程倒計時,兩個不同的時間。 – Pheonix7 2012-04-23 14:18:37

+0

@Hassan你將不得不使用像我上面發佈的代碼來測試中斷標誌和'InterruptedException'。 – Gray 2012-04-23 14:19:44

+0

好的,我現在就試試 – Pheonix7 2012-04-23 14:21:48