java handler的用法_Handler的基本用法

本文介绍了一个使用Handler进行消息传递的示例应用。该应用通过发送不同类型的Message来更新UI,展示了如何利用Handler处理异步任务并接收反馈。具体包括空消息、带参数的消息等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Handler 的Send message ,可以传: what , arg1 ,arg2 ,obj :

package com.example.userrecyclerviewapplication;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.ProtocolException;

import java.net.URL;

import java.util.List;

import java.util.Random;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

private Button send, upload;

private TextView txt;

private String str = "";

private Handler handler = new Handler() {

@Override

public void handleMessage(@NonNull Message msg) {

super.handleMessage(msg);

if(msg.what == 1){

txt.setText(str);

}else if(msg.what == 2){

String str2 = "What : "+ msg.what+";arg1:"+msg.arg1+";arg2:"+msg.arg2+";obj:"+((Random)msg.obj).nextInt();

txt.setText(str2);

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initEvent();

}

private void initView() {

send = findViewById(R.id.send_request);

upload = findViewById(R.id.upload);

txt = findViewById(R.id.txt);

}

private void initEvent() {

upload.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new Thread(new Runnable() {

@Override

public void run() {

Message message = new Message();

message.what = 2;

message.arg1 = 111;

message.arg2 = 222;

message.obj = new Random();

handler.sendMessage(message);

}

}){}.start();

}

});

send.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new Thread(new Runnable() {

@Override

public void run() {

String msg = get();

handler.sendEmptyMessage(1);

}

}) {

}.start();

}

});

}

private String get() {

try {

URL url = new URL("http://10.0.2.2/star_male.json");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection.setConnectTimeout(6000);

if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

InputStream in = connection.getInputStream();

byte[] b = new byte[1024];

int len = 0;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while ((len = in.read(b)) > -1) {

baos.write(b, 0, len);

}

str = new String(baos.toByteArray());

return str;

}

} catch (ProtocolException e) {

e.printStackTrace();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值