2010-09-12 59 views
4

嘿所有 - 这个代码可能有点凌乱我试图清理处理程序,因为我一直在追踪发生崩溃的地方...如何杀死一个线程和处理程序前去新的活动

我有一个是表示由线程和处理程序进行动画与进度密码输入对话框活动...

看来,当我试图看看是否进度已经完成,并试图杀死线程,当我尝试去一个新的活动 - 即以某种方式调用一个函数,并没有任何东西返回什么的时候,我这样做的方式正在搞乱一些东西。 ..

public class RMO_Dialog extends Activity { 
    private ProgressBar progbar; 
    private Button dialogOK; 
    private EditText dialogPass; 
    private SharedPreferences prefs; 
    private String pass; 
    private int increment=10; 
    private Thread background; 

    private Boolean commCalled=false; 

    public void callCommunications(){ 
     progbar.setVisibility(0); 
     progbar.setProgress(0); 
     background.stop(); 
     Toast.makeText(getApplicationContext(), "Call communication should happen once.", Toast.LENGTH_LONG).show(); 
//  Intent i = new Intent(); 
//  i.setClass(RMO_Dialog.this, RMO_Comm.class); 
//  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//  startActivity(i); 
//   finish(); 
    } 

    public void buzzUser(){ 

     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
     int dot = 200; 
     int dash = 500; 
     int short_gap = 200; 
     int medium_gap = 500; 
     int long_gap = 1000; 
     long[] pattern = {0,dot, short_gap, dot, short_gap, dot, medium_gap, dash, short_gap, dash, short_gap, dash, medium_gap, dot, short_gap, 
       dot, short_gap, dot, long_gap}; 

     v.vibrate(pattern, -1); 


    } 

    public void killCountdown(){ 
     progbar.setVisibility(0); 
     progbar.setProgress(0); 
     background.stop(); 
    } 

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

     buzzUser(); 

     prefs = this.getSharedPreferences("RMO", MODE_WORLD_READABLE); 
     pass = prefs.getString("password", ""); 

     dialogOK = (Button) findViewById(R.id.dialogOK); 
     dialogPass = (EditText) findViewById(R.id.dialogPass); 
     progbar = (ProgressBar) findViewById(R.id.progress); 

     progbar.setProgress(0); 

     background = new Thread(new Runnable(){ 
      @Override 
      public void run() { 
       try{ 
        while(progbar.getProgress()<=progbar.getMax()){ 
         Thread.sleep(300); 
         progressHandler.sendMessage(progressHandler.obtainMessage()); 
        } 
       }catch(java.lang.InterruptedException e){ 
        Toast.makeText(getApplicationContext(), "Error thrown.", Toast.LENGTH_LONG).show(); 
       } 

      } 

     }); 
     background.start(); 

     dialogOK.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(dialogPass.getText().toString().equals(pass.toString())){ 
        killCountdown(); 
        Toast.makeText(getApplicationContext(), "Guardian Angel next alert has been disengaged.", Toast.LENGTH_LONG).show(); 
        Intent intent = new Intent(); 
        intent.setClass(RMO_Dialog.this, RMO.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        finish(); 
       }else{ 
        callCommunications(); 
       } 
      } 
     }); 



    } 

    Handler progressHandler = new Handler(){ 
     public void handleMessage(Message msg){ 
      progbar.incrementProgressBy(increment); 
      if(progbar.getProgress()==progbar.getMax()){ 
       Toast.makeText(getApplicationContext(), "commcalled: "+ commCalled, Toast.LENGTH_LONG).show(); 
       if(commCalled==false){ 
        commCalled=true; 
        callCommunications(); 
       } 

      } 
     } 
    }; 
} 
+0

我修改了一下你的格式,不要忘了选择你的代码并点击*代码格式化按钮*(101010)...你也可以通过点击* *在问题下面编辑**。 – Kiril 2010-09-12 20:46:24

回答

7

Thread.stop is deprecated call,而应该使用Thread.interrupt方法。

public void killCountdown(int waitTime){ 
    progbar.setVisibility(0); 
    progbar.setProgress(0); 
    // deprecated: background.stop(); 
    background.interrupt(); // <-- OK 
    background.join(waitTime); // optionally wait for the thread to exit 
} 

Thread.Interrupt会导致ThreadInterruptedException下一次你的线程块或睡觉,你就已经在你的体内线程处理,这样是很好的。另外,您可能需要包含一个volatile标志,该标志允许您在线程未被阻止或休眠时停止该线程,但这是可选的。

+0

很好的解释,特别是我没有在我的Android应用程序中处理的异常。你帮我理解谢谢! – Zonata 2012-07-23 14:19:29

+0

@Zonata太棒了!另外,请不要忘记通过点击左侧的复选标记(就在投票下)接受最能解决您问题的问题。 – Kiril 2012-07-23 14:38:06

+0

Lirik - 我会的,但这不是我的问题;) – Zonata 2012-07-23 14:45:03

2

你可能会考虑使用一个AsyncTask实例,而不是一个可运行的和一个处理程序。

如果您需要取消AsycnTask实例只需拨打.cancel(true)您的AsyncTask对象引用。这将处理背景方法(doInBackground())和进度更新程序(onProgressUpdate())。

我通常发现AsyncTask比尝试自己处理所有细节更容易使用。

所以,里面RMO_Dialog,在一个类的实例创建使用呼叫​​延伸的AsyncTask。

public class RMO_Dialog extends Activity { 

    ... 
    // Get ref to your bg task for easily cancellation if needed 
    PassWordEntry background = new PassWordEntry(); 
    // Start bg task 
    background.execute([PARAMS]); 
    ... 
    // Cancel task 
    background.cancel(true); 
    ... 

    // AsyncTask lets you encapsulate both your runnable and handler in it 
    private static class PassWordEntry() extends AsyncTask<[PARAMS], [PROGRESS], [RESULT]> { 
     protected [RESULT] doInBackground() { 
      ... // Runnable stuff here 
      return [RESULT]; 
     } 

     protected void onProgressUpdate([PROGRESS]... progress) { 
      ... // progressHandler stuff here 
     } 

     protected void onPostExecute([RESULT]) {   
      // Clean up return data when all done w BG here 
     } 
    } 

}