您好,登錄后才能下訂單哦!
1. 原理
每一個(gè)線程對(duì)應(yīng)一個(gè)消息隊(duì)列MessageQueue,實(shí)現(xiàn)線程之間的通信,可通過Handler對(duì)象將數(shù)據(jù)裝進(jìn)Message中,再將消息加入消息隊(duì)列,而后線程會(huì)依次處理消息隊(duì)列中的消息。
2. Message
初始化:一般使用Message.obtain()方法獲取一個(gè)消息對(duì)象,該方法會(huì)檢查Message對(duì)象池中是否存在可重復(fù)利用的對(duì)象,若無,才會(huì)new一個(gè)新對(duì)象。
what:相當(dāng)于Message的標(biāo)識(shí)符,區(qū)別于其它消息。
arg1、arg2:int類型,可傳遞整數(shù)。
obj:object類型,可傳遞任意對(duì)象。
3. 發(fā)送消息
在子線程中可調(diào)用主線程的handler.sendMessage(msg)
進(jìn)行發(fā)送消息,經(jīng)過一系列方法調(diào)用,會(huì)觸發(fā)handler的handleMessage方法,從而進(jìn)行消息處理。
發(fā)送消息的主要方法:
handler.sendMessage(Message msg); handler.sendMessageAtTime(Message msg, int time); handler.sendMessageDelayed(Message msg, int time);
sendMessageAtTime()
和sendMessageDelayed()
區(qū)別在于前者是在指定時(shí)間發(fā)送消息,可配合SystemClock.uptimeMillis()
使用;而后者則是延時(shí)發(fā)送消息。
除了SendMessage()方法以外,還可以通過post()方法發(fā)送消息:
handler.post(Runnable r); handler.postDelayed(Runnable r, int time);
sendMessage()與post()的區(qū)別:https://www.jb51.net/article/120624.htm
4. 內(nèi)存泄漏
https://www.jb51.net/article/120627.htm
5. 通過Handler對(duì)象實(shí)現(xiàn)下載文件動(dòng)態(tài)更新進(jìn)度條
AndroidManifest加入權(quán)限聲明:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.studying.network.DownloadActivity"> <ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> <Button android:id="@+id/download" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/download" /> </LinearLayout>
Activity:
public class DownloadActivity extends Activity { private static final int DOWNLOAD_MESSAGE_CODE = 100001; private static final int DOWNLOAD_MESSAGE_FAIL_CODE = 100002; private static final String APP_URL = "http://clfile.imooc.com/class/assist/119/1328281/Android%20Studio%20教輔%20.pdf"; private MyHandler mHandler; private ProgressBar mProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download); findViewById(R.id.download).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //開啟子線程 new Thread(new Runnable() { @Override public void run() { download(APP_URL); } }).start(); } }); mProgressBar = (ProgressBar) findViewById(R.id.progress_bar); mHandler = new MyHandler(this); } private void download(String appUrl) { try { URL url = new URL(appUrl); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); int contentLength = conn.getContentLength();//獲取文件總大小 String downloadPath = Environment.getExternalStorageDirectory() + File.separator + "imooc" + File.separator; File file = new File(downloadPath); if (!file.exists()) { file.mkdir(); } String fileName = downloadPath + "test.pdf"; File apkFile = new File(fileName); if (apkFile.exists()) { apkFile.delete(); } int downloadSize = 0;//記錄已經(jīng)下載的大小 byte[] bytes = new byte[1024]; int length = 0; OutputStream out = new FileOutputStream(fileName); while ((length = in.read(bytes)) != -1) { out.write(bytes, 0, length); downloadSize += length; Message msg = Message.obtain(); msg.obj = downloadSize / contentLength * 100;//progress的值為0到100,因此得到的百分?jǐn)?shù)要乘以100 msg.what = DOWNLOAD_MESSAGE_CODE; mHandler.sendMessage(msg); } in.close(); out.close(); } catch (IOException e) { notifyDownloadFailed(); e.printStackTrace(); } } private void notifyDownloadFailed() { Message msg = Message.obtain(); msg.what = DOWNLOAD_MESSAGE_FAIL_CODE; mHandler.sendMessage(msg); } private static class MyHandler extends Handler{ private WeakReference<DownloadActivity> weakReference; MyHandler(DownloadActivity activity) { this.weakReference = new WeakReference<>(activity);//以弱引用的形式傳遞Activity,避免內(nèi)存泄漏 } @Override public void handleMessage(Message msg) { super.handleMessage(msg); DownloadActivity activity = weakReference.get(); //消息處理 switch (msg.what) { case DOWNLOAD_MESSAGE_CODE: activity.mProgressBar.setProgress((Integer) msg.obj); break; case DOWNLOAD_MESSAGE_FAIL_CODE: Toast.makeText(activity, "下載失?。?, Toast.LENGTH_SHORT).show(); break; } } } }
總結(jié)
以上所述是小編給大家介紹的Handler實(shí)現(xiàn)線程之間的通信下載文件動(dòng)態(tài)更新進(jìn)度條,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。