2013-10-05 55 views
0

我正在開發Android平臺上的DES解密。android - 進度黑屏但仍在運行

這是我的主要

package com.example.crack; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class Main extends Activity { 

    public final static String EXTRA_MESSAGE = "com.example.crack.MESSAGE"; 
    public final static String EXTRA_PLAINTEXT = "com.example.crack.PLAINTEXT"; 
    public final static int ENCRYPTION_REQUEST = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void sendMessage(View view) { 
     Intent intent = new Intent(this, encryption.class); 
     EditText editText = (EditText) findViewById(R.id.input_message); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivityForResult(intent, ENCRYPTION_REQUEST); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // Check which request it is that we're responding to 
     if (requestCode == ENCRYPTION_REQUEST) { 
      // Make sure the request was successful 
      if (resultCode == RESULT_OK) { 
       String result = data.getStringExtra(encryption.EXTRA_ENCRYPTION_RETURN); 

       Intent intent = new Intent(this, DisplayMessage.class); 
       intent.putExtra(EXTRA_MESSAGE, result); 
       startActivity(intent); 
      } 
     } 
    } 
} 

,這是部分我encrpytion的

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.encryption); 

    Intent intent = getIntent(); 
    message = intent.getStringExtra(Main.EXTRA_MESSAGE); 

    //Dictionary 
    is = getResources().openRawResource(R.raw.english); 
    in = new BufferedReader(new InputStreamReader(is)); 
    readDic(); 

    String result = ""; 
    try { 
     result = decryptBruteForce(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    Intent returnIntent = new Intent(); 
    returnIntent.putExtra(EXTRA_ENCRYPTION_RETURN,result); 
    setResult(RESULT_OK,returnIntent);  
    finish(); 

} 

當我點擊按鈕,它會調用SendMessage函數,在其運行時的解密屏幕只是黑屏,直到它完成運行。

我曾嘗試使用進度條,按照此guide,但不工作,我需要一個按鈕,可以停止運行時的過程。

是否可以設置登錄視圖,它顯示了該功能現在正在執行的操作?就像IDE日誌中顯示的那樣?例如,顯示現在解密的密鑰是什麼。

或者,也許只是一個進度條或請等待也會。

我試圖改變的sendMessage這一點,但它仍然黑掉和崩潰

public void sendMessage(View view) { 
     final Intent intent = new Intent(this, encryption.class); 
     view.setEnabled(false); 
     AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { 

      @Override 
      protected void onPreExecute() { 
       pd = new ProgressDialog(context); 
       pd.setTitle("Processing..."); 
       pd.setMessage("Please wait."); 
       pd.setCancelable(false); 
       pd.setIndeterminate(true); 
       pd.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... arg0) { 
       try { 
        //Do something... 

        EditText editText = (EditText) findViewById(R.id.input_message); 
        String message = editText.getText().toString(); 
        intent.putExtra(EXTRA_MESSAGE, message); 
        startActivityForResult(intent, ENCRYPTION_REQUEST); 

        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       if (pd!=null) { 
        pd.dismiss(); 
        b.setEnabled(true); 
       } 
      } 

     }; 
     task.execute((Void[])null); 
    } 

如果我把睡眠50000,它沒有崩潰,但仍熄滅。

+0

和你的日誌應用程序崩潰的貓秀? –

回答

1

你可以使用Thread和Handler來完成。在您嘗試每種組合時,您都會更新進度欄。

private int mProgressStatus = 0; 
private Handler mHandler = new Handler(); 

protected void onCreate(Bundle savedInstanceState)  
{  
    .... // Other initializations 

mProgress = (ProgressBar) findViewById(R.id.progress_bar); 
mProgress.setMax(dictionaryLength); 

// Start lengthy operation in a background thread 
new Thread(new Runnable() { 
public void run() { 
    for (int i=0 ; i<dictionaryLength ; i++) 
    { 
     mProgressStatus = decryptBruteForce(i); 

     // Update the progress bar 
     mHandler.post(new Runnable() { 
      public void run() { 
       mProgress.setProgress(mProgressStatus); 
      } 
     }); 
    } 
} 
}).start(); 
} 

不過我建議你使用的AsyncTask做後臺操作,而你需要更新用戶界面以顯示有關怎麼回事進度或信息。

http://developer.android.com/intl/es/reference/android/os/AsyncTask.html

它是一種良好的習慣來添加一個取消控制在你的循環,這樣就可以從的AsyncTask外完成它(例如,在你的UI另一個按鈕)。

private class DecryptTask extends AsyncTask<String, Integer, Long> { 
protected Long doInBackground(String... words) 
{ 
    long wordsDecrypted = 0; 
    for (int i = 0; i < words.length ; i++) { 
     wordsDecrypted += decryptBruteForce(i); 
     publishProgress(i); 

     // Escape early if cancel() is called 
     if (isCancelled()) 
      break; 
    } 
    return wordsDecrypted; 
} 

protected void onProgressUpdate(Integer... progress) { 
    mProgress.setProgress(progress[0]); 
} 

protected void onPostExecute(Long result) { 
    showDialog("Decrypted " + result + " words"); 
} 
} 

而且你可以從外部取消的AsyncTask與取消方法:

http://developer.android.com/intl/es/reference/android/os/AsyncTask.html#cancel(boolean)

PD:代碼沒有經過測試,只是例子,說明它是如何工作