Android(安卓)多线程
16lz
2022-06-29
1、简介
多线程的用途是不言而喻的,例如,我们下载一个文件,在下载过程当前我们又要执行其他的操作。如果都放在主线程中,UI界面将不能操作,需要等待。
2、Android 平台下的多线程
Android 平台下的线程分为主线程(也叫UI线程) 和 工作线程(非UI线程)。在Android 平台中 非UI线程 是不能访问UI线程中的View组件的。这个必须清楚。例如我们执行下面的代码就会报错。这个例子我们模拟一个下载场景,有一个下载按钮,一个其他操作按钮,一个TextView显示当前下载状态,当我们在工作线程中更新TextView文本内容是就会报错。
package com.powerise.thread;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button downBtn;private TextView stateTextView;private DownloadThread dt;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); downBtn = (Button) findViewById(R.id.downBtn); stateTextView = (TextView) findViewById(R.id.stateTextView); dt = new DownloadThread(); downBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dt.start();}}); }private final class DownloadThread extends Thread {@Overridepublic void run() {stateTextView.setText("下载中...");try {Thread.sleep(5000);} catch (Exception e) {}stateTextView.setText("下载完成!");}}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="@+id/stateTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /><Button android:id="@+id/downBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="downBtn" /></LinearLayout>
为了实现UI线程和工作线程之间的通信我们需要使用Handler对象发送消息和处理消息。
package com.powerise.thread;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button downBtn;private TextView stateTextView;private DownloadThread dt;private DownloadHandler dh = new DownloadHandler();@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); downBtn = (Button) findViewById(R.id.downBtn); stateTextView = (TextView) findViewById(R.id.stateTextView); dt = new DownloadThread(); downBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dt.start();}}); }private final class DownloadThread extends Thread {@Overridepublic void run() {dh.sendEmptyMessage(1);try {Thread.sleep(5000);} catch (Exception e) {}dh.sendEmptyMessage(-1);}}private final class DownloadHandler extends Handler {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case 1:stateTextView.setText("下载中...");break;case -1:stateTextView.setText("下载完成!");break;default :break;}}}}
还有问题, 就是当不停的点 Button 时, 也会报错.
更多相关文章
- 使用NetBeans搭建Android开发环境
- android studio Could not find com.android.support.constraint
- 浅析Android中的消息机制-解决:Only the original thread that cr
- Android异步消息机制之Handler
- Android的Handler机制详解3_Looper.looper()不会卡死主线程
- Android下Excel的操作
- android源码下载方式
- Android之Handler用法总结
- 【Android】Android(安卓)相关下载