new Thread(runnable).start();
用这个开始一个线程
static Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String val = data.getString("username"); if (val != null) { Log.i("Exception", "请求结果-->" + val); } } };
用这个handler处理UI线程的更新
Runnable runnable = new Runnable() { @Override public void run() { // if(validate()){ Log.i("Exception", "validate ok"); editor = sharedPreferences.edit(); String username = userAccount.getText().toString(); Log.i("iwork",username); editor.putString("username", username); editor.commit(); Intent intent = new Intent(LoginActivity.this, HomeworkListActivity.class); startActivity(intent); Message msg = new Message(); Bundle data = new Bundle(); data.putString("username", sharedPreferences.getString("username", null)); msg.setData(data); Log.i("iwork","sendMessage data:"+msg.getData().getString("username")); handler.sendMessage(msg); } };
再实现runnable,实例一个线程