2011-02-04 56 views
0

我的程序運行正常,但在程序運行過程中,用戶單擊BACK按鈕,後退按鈕的功能不起作用。但是在完成執行後,BACK按鈕功能正在工作。在運行我的程序時,後退按鈕功能不能工作(Android)

請在這裏觀察我的代碼。

package com.sampleexample; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SampleExample extends Activity { 
    static final int PROGRESS_DIALOG = 0; 
    Button button; 
    TextView download; 
    ProgressThread progressThread; 
    ProgressDialog progressDialog; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.d("SampleExample", "======= onCreate()====Start===="); 
     // Setup the button that starts the progress dialog 
     download = (TextView) findViewById(R.id.download); 
     button = (Button) findViewById(R.id.progressDialog); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Log.d("SampleExample", "======= onClick()==Start======"); 
       showDialog(PROGRESS_DIALOG); 
       Log.d("SampleExample", "======= onClick()===End====="); 
      } 
     }); 
     Log.d("SampleExample", "======= onCreate()====End===="); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     // Handle the back button 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      // Ask the user if they want to quit 
      Log.d("SampleExample", " ------- Back Button onKeyDown()----Start--"); 
      new AlertDialog.Builder(this).setIcon(
        android.R.drawable.ic_dialog_alert).setTitle("Exit") 
        .setMessage("Are you sure you want to leave?") 
        .setNegativeButton(android.R.string.cancel, null) 
        .setPositiveButton(android.R.string.ok, 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // Exit the activity 
            SampleExample.this.finish(); 
           } 
          }).show(); 

      // Say that we've consumed the event 
      Log.d("SampleExample", " ------BackButton-onKeyDown()----End--"); 
      return true; 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

    // Toast.makeText(SampleExample.this, "U have pressed the Back Button", 
    // Toast.LENGTH_SHORT).show(); 

    // Log.d(this.getClass().getName(), 
    // "*********back button pressed----------"); 

    protected Dialog onCreateDialog(int id) { 
     Log.d("SampleExample", "99999 onCreateDialog() 999 Start"); 
     switch (id) { 
     case PROGRESS_DIALOG: 
      progressDialog = new ProgressDialog(SampleExample.this); 
      progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      progressDialog.setMessage("Loading..."); 
      download.setText("Now downloading......."); 
      progressThread = new ProgressThread(handler); 
      progressThread.start(); 
      return progressDialog; 
     default: 
      return null; 
     } 
    } 

    // Define the Handler that receives messages from the thread and update the 
    // progress 
    final Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 
      Log.d("SampleExample", "8888 handleMessage() 8888 Start"); 
      int total = msg.getData().getInt("total"); 
      progressDialog.setProgress(total); 
      if (total >= 100) { 
       // Toast.makeText(SampleExample.this, "Download is completed", 
       // Toast.LENGTH_SHORT).show(); 
       download.setText(" download is completed."); 
       dismissDialog(PROGRESS_DIALOG); 
       progressThread.setState(ProgressThread.STATE_DONE); 
      } 
     } 
    }; 

    /** Nested class that performs progress calculations (counting) */ 
    private class ProgressThread extends Thread { 
     Handler mHandler; 
     final static int STATE_DONE = 0; 
     final static int STATE_RUNNING = 1; 
     int mState; 
     int total; 

     ProgressThread(Handler h) { 
      mHandler = h; 
     } 

     public void run() { 
      mState = STATE_RUNNING; 
      total = 0; 
      Log.d("SampleExample", "7777 run() 7777 Start"); 
      while (mState == STATE_RUNNING) { 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        Log.e("ERROR", "Thread Interrupted"); 
       } 
       Message msg = mHandler.obtainMessage(); 
       Bundle b = new Bundle(); 
       b.putInt("total", total); 
       msg.setData(b); 
       mHandler.sendMessage(msg); 

       total++; 
      } 
      Log.d("SampleExample", "6666 run() 6666 End"); 
     } 

     /* 
     * sets the current state for the thread, used to stop the thread 
     */ 
     public void setState(int state) { 
      mState = state; 
     } 
    } 
} 
+1

平滑下來你問的問題。它的痛苦閱讀此代碼 – Javanator 2011-02-04 07:52:10

回答

1

你還沒有真正解釋你的問題的症狀很明顯,但是從你的代碼看起來你踢的後臺線程,並彈出一個ProgressDialog。我假設它正在運行,您正試圖按BACK鍵。這似乎沒有做任何事情的原因是Dialog默認情況下不能使用BACK鍵取消。您必須致電setCancelable(true)才能啓用此行爲。

case PROGRESS_DIALOG: 
     progressDialog = new ProgressDialog(SampleExample.this); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDialog.setMessage("Loading..."); 
     progressDialog.setCancelable(true); 
     ... 
     return progressDialog; 

這將允許ProgressDialog被BACK按鈕取消。但是,即使Dialog已被取消,您的背景Thread仍將繼續運行。

0

chandu你有沒有張貼問題

重新檢查下面的代碼在你的處理器

if (total >= 100) { 

    download.setText(" download is completed."); 
    dismissDialog(PROGRESS_DIALOG); 
    progressThread.setState(ProgressThread.STATE_DONE); 
} 

您的代碼將無法達到dismissDialog直到總> = 100 &因此,即使同時該程序前檢查你的邏輯您無法useyour返回的行動

&也請停止發佈multiple questions

1
// handle phone back button 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) {  
      return quitWithPID(); 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

// reusable method (modifier may be public..) 
private boolean quitWithPID() { 
    new AlertDialog.Builder(this).setIcon(
      android.R.drawable.ic_dialog_alert).setTitle("Exit Boot Options?") 
      .setMessage("Are you sure you want to exit?") 
      .setNegativeButton(android.R.string.cancel, null) 
      .setPositiveButton(android.R.string.ok, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // Exit the activity 
          MainActivity.this.finish(); 
          // Terminate the process ;) 
          android.os.Process.killProcess(android.os.Process.myPid()); 
         } 
        }).show();   
    return true; 
}