0

我正在Android平台上开发DES解密。

这是我的主要

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

这是我加密的一部分

@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 函数,而它正在运行解密屏幕只是黑屏,直到它完成运行。

我曾尝试按照本指南使用进度条,但不起作用,我需要一个可以在运行时停止进程的按钮。

是否可以设置一个登录视图,显示该功能现在正在做什么?就像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,它不会崩溃,但仍然会黑屏。

4

1 回答 1

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

但是,我建议您在需要更新 UI 以显示进度或有关正在发生的事情的信息时使用 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");
 }
}

您可以使用 cancel 方法从外部取消 AsyncTask:

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

PD:代码未经测试,仅举例说明其工作原理

于 2013-10-05T08:48:52.570 回答