Java 获取int到UI线程
我正在开发一个Android应用程序,它可以强制使用从Java 获取int到UI线程,java,android,multithreading,Java,Android,Multithreading,我正在开发一个Android应用程序,它可以强制使用从int创建的MD5求和 蛮力部分工作正常。(我可以sysout最终值,它是正确的。) 我在将输出值放入警报对话框时遇到问题。Logcat说:试图在主线程之外初始化硬件加速,中止 它正在中止我代码中的最后一条语句,即实际显示警报对话框的语句 builder.show(); 下面是我的MainActivity.java: import android.app.Activity; import android.app.AlertDialog; i
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();
}
}
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Looper;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.EditText;
导入android.widget.RadioButton;
导入android.widget.Toast;
导入java.io.UnsupportedEncodingException;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
公共类MainActivity扩展了活动{
字符串密码tohash;
字符串结果;
布尔值goodPIN=false;
布尔startbruteforce=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//我的东西
public void doIt(视图v)抛出NoSuchAlgorithmException、UnsupportedEncodingException
{
RadioButton r2=(RadioButton)findViewById(R.id.calculate);
RadioButton r1=(RadioButton)findViewById(R.id.crack);
最终EditText输入=(EditText)findViewById(R.id.inputTextArea);
最终EditText输出=(EditText)findViewById(R.id.outputTextArea);
//Toast.makeText(这是“正在努力!”,Toast.LENGTH\u LONG.show();
if(r2.isChecked())
{
if(input.getText().toString().length()>4)
{
goodPIN=false;
output.setText(“”);
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“uuuuuhh…”);
setMessage(“未计算哈希值,因为该PIN需要太长时间才能强制执行:(”);
setPositiveButton(“是的,不管什么…”,null);
builder.show();
}
其他的
{
goodPIN=true;
}
if(goodPIN)
{
视图=this.getCurrentFocus();
如果(视图!=null){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
Toast.makeText(这是“计算MD5!”,Toast.LENGTH_LONG.show();
passwordToHash=input.getText().toString();
MessageDigest=MessageDigest.getInstance(“MD5”);
byte[]inputBytes=passwordToHash.getBytes(“UTF-8”);
byte[]hashBytes=digest.digest(inputBytes);
StringBuffer StringBuffer=新的StringBuffer();
for(int i=0;irunOnUiThread(new Runnable() {
@Override
public void run() {
}
});
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);
}
}