android 线程

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,实例一个线程

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。