java - 进入 UI 线程

标签 java android multithreading

我正在开发一个 Android 应用程序,该应用程序会暴力破解从 int 创建的 MD5 和。

暴力破解部分工作正常。 (我可以sysout最终值并且它是正确的。)

我在将输出值发送到警报对话框时遇到问题。 Logcat 说:尝试在主线程之外初始化硬件加速,正在中止

它在我的代码中的最后一条语句(实际显示警报对话框的语句)处中止;

builder.show();

这是我的 MainActivity.java:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends Activity {

    String passwordToHash;
    String result;
    boolean goodPIN = false;
    boolean startbruteforce = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //My stuff

    public void doIt(View v) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
        RadioButton r2 = (RadioButton) findViewById(R.id.calculate);
        RadioButton r1 = (RadioButton) findViewById(R.id.crack);

        final EditText input = (EditText) findViewById(R.id.inputTextArea);
        final EditText output = (EditText) findViewById(R.id.outputTextArea);

        //Toast.makeText(this, "Working on it!", Toast.LENGTH_LONG).show();

        if(r2.isChecked())
        {
            if(input.getText().toString().length() > 4)
            {
                goodPIN = false;
                output.setText("");
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle ("Uuuuuuhh....");
                builder.setMessage("Hash not calculated because that PIN would take too long to brute force :(");
                builder.setPositiveButton("Yeah, whatever...", null);
                builder.show();
            }
            else
            {
                goodPIN = true;
            }

            if(goodPIN)
            {
                View view = this.getCurrentFocus();
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }

                Toast.makeText(this, "Calculated MD5!", Toast.LENGTH_LONG).show();

                passwordToHash = input.getText().toString();

                MessageDigest digest = MessageDigest.getInstance("MD5");

                byte[] inputBytes = passwordToHash.getBytes("UTF-8");

                byte[] hashBytes = digest.digest(inputBytes);

                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < hashBytes.length; i++)
                {
                    stringBuffer.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16)
                            .substring(1));
                }

                result = stringBuffer.toString();

                output.setText(result);
            }
        }


        else if(r1.isChecked())
        {
            View view = this.getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }

            final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "Working on it!", "Brute-forcing. Please wait...", true);
            double starttime = System.currentTimeMillis();

            final Thread thread = new Thread()
            {
                @Override
                public void run()
                {
                    String crackedPassword = "Hello";
                    String crackedPasswordHash = "a262";
                    int pinsTested = 1000;
                    int crackedPasswordInt = 1000;
                    String passwordToCrack;

                    //Get the password to crack
                    passwordToCrack = input.getText().toString();

                    long startTime = System.currentTimeMillis();

                    while (!crackedPasswordHash.equals(passwordToCrack))
                    {
                        pinsTested++;
                        crackedPasswordInt++;
                        crackedPassword = Integer.toString(crackedPasswordInt);

                        MessageDigest digest = null;
                        try
                        {
                            digest = MessageDigest.getInstance("MD5");
                        }
                        catch (NoSuchAlgorithmException e)
                        {
                            e.printStackTrace();
                        }

                        byte[] inputBytes = new byte[0];
                        try
                        {
                            inputBytes = crackedPassword.getBytes("UTF-8");
                        }
                        catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        byte[] hashBytes = digest.digest(inputBytes);

                        StringBuffer stringBuffer = new StringBuffer();
                        for (int i = 0; i < hashBytes.length; i++)
                        {
                            stringBuffer.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16)
                                    .substring(1));
                        }

                        crackedPasswordHash = stringBuffer.toString();

                        //System.out.println(pinsTested + " PINs tested");
                        //System.out.println("Hash of: " + pinsTested + " is: " + crackedPasswordHash);
                    }
                    long endTime = System.currentTimeMillis();
                    long totalTime = endTime - startTime;

                    System.out.println("Done! " + pinsTested);

                    updateUI(pinsTested);

                    //runOnUiThread(pinsTested);
                }
            };

            Thread animation = new Thread()
            {
                @Override
                public void run()
                {
                    try
                    {
                        Thread.sleep(4000);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    dialog.dismiss();
                    thread.start();
                }
            };

            animation.start();

        }
    }

    public void updateUI(final int pass) {

        Looper.prepare();
        final Handler myHandler = new Handler();
            (new Thread(new Runnable() {

                @Override
                public void run() {
                    myHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            test(pass);
                        }
                    });
                }
        })).start();
    }

    public void test(int pass)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle ("Done!");
        builder.setMessage("PIN is: " + pass);
        builder.setPositiveButton("Yeah, whatever...", null);
        builder.show();
    }
}

最佳答案

作为UI Thread说:要将数据从后台线程移动到 UI 线程,请使用在 UI 线程上运行的处理程序。
你在 updateUI 方法中创建了 Handler ,但是 updateUI 是在 UI Thread 之外的线程中创建的,所以你得到了错误。
你需要这样尝试:

public class MainActivity extends Activity {
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
               case 1:
               test((int)msg.obj);
            }
        }
    }

    public void updateUI(final int pass) {
         Message msg = Message.obtain();
         msg.what=1;
         msg.obj = pass;
         mHandler.sendMessage(msg);
    }
}

关于java - 进入 UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36254242/

相关文章:

android - 使用 selectionArgs 在查询中不起作用

java - java中的多线程,等待困难

java - 在 Java 中构造 URI 时保留转义字符

java - Spring + mybatis = java.lang.NullPointerException

java - 如何在 Java 中使用单选按钮

小部件的Android菜单

java - 如何在java中给一个巨大的数字加电?

java - 在BACKGROUND中通过android AlarmClock类设置闹钟

asp.net - ASP.NET HttpModules 中的线程安全缓存

非阻塞 Win32 系统调用(如 ReleaseMutex)是否会导致线程阻塞并允许较低优先级的线程运行?