Java 如何从线程类将文本设置为文本视图
我正在逐块读取inputstream chunck,并尝试从线程类将read chunk设置为textview,但文本仅在完成循环后打印,下面是我的代码:Java 如何从线程类将文本设置为文本视图,java,android,multithreading,ui-thread,Java,Android,Multithreading,Ui Thread,我正在逐块读取inputstream chunck,并尝试从线程类将read chunk设置为textview,但文本仅在完成循环后打印,下面是我的代码: class SendFileThread extends Thread { Handler mHandler; FileInputStream instream; SendFileThread(Handler h, FileInputStream stream ) { mHandler
class SendFileThread extends Thread
{
Handler mHandler;
FileInputStream instream;
SendFileThread(Handler h, FileInputStream stream )
{
mHandler = h;
instream = stream;
this.setPriority(Thread.MAX_PRIORITY);
}
@Override
public void run()
{
final StringBuilder result = new StringBuilder();
Message msg;
byte [] usbdata = new byte[64];
int readcount = 0;
sendByteCount = 0;
int val = 0;
if(instream != null)
{
try
{
readcount = instream.read(usbdata,0,64);
}
catch (IOException e){e.printStackTrace();}
while(readcount > 0)
{
sendData(readcount, usbdata);
sendByteCount += readcount;
try
{
readcount = instream.read(usbdata,0,64);
if(readcount == -1){
pending = false;
//send_file = false;
setDefaultsBoo("pending",pending, J2xxHyperTerm.this);
}else{
result.append(new String(usbdata, 0, readcount));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
readText.setMovementMethod(new ScrollingMovementMethod());
readText.setText(result.toString());
//scrollView.smoothScrollTo(0, readText.getHeight() + 30);
}
});
}
catch (IOException e){e.printStackTrace();}
}
}
}
}
只有在所有工作完成后,文本才会设置为文本视图。工作完成后,请尝试以下操作-
Message msg = Message.obtain(handler, what, arg1, arg2, "text");
// what= some int identififer, lets say 1101
msg.sendToTarget();
在侦听活动中,执行处理程序.Callback
。您必须实现handleMessage
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 1104: // the what identifier you set in message
String text = (String) msg.obj;
textView.setText(text)
break;
}
return false;
}
您可以在这个用例中使用AsyncTask,因为在UI线程上更新视图的功能是现成的 这来自所提供的用法示例,带有注释,用于显示主线程操作发生的位置
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
// Done in the background, on a separate thread
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
// This part of the loop publishes the progress to onProgressUpdate(..)
publishProgress((int) ((i / (float) count) * 100));
if (isCancelled()) break;
}
return totalSize;
}
// Called on the main thread whenever publishProgress(..) is called
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
// Also called on the main thread, after the background task is finished
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
私有类下载文件任务扩展异步任务{
//在后台,在单独的线程上完成
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
您好,您可以在线程中使用此方法:
runOnUiThread(new Runnable() {
@Override
public void run() {
//update your textview
}
});
您可以使用EventBus在线程和活动之间进行通信。
是他们的github项目。您可以实现它,还可以启用每个Android应用程序组件之间的通信。例如,就像您的情况一样,在线程和活动之间 视图只能由创建它的线程来访问。TextView是由主线程创建的,所以您不能从另一个线程更新/更改状态(如上所述)。但是,您可以从线程发送事件以使用Handler/Looper.getMainLooper/RunOnUiThread(如果在活动上下文中),以便主线程将更新TextView。