Android使用AsyncTask實(shí)現(xiàn)多線程下載的方法
本文實(shí)例講述了Android使用AsyncTask實(shí)現(xiàn)多線程下載的方法。分享給大家供大家參考,具體如下:
public class MainActivity extends Activity implements OnClickListener { private Button btn1, btn2, btn3; private ProgressBar progressBar1, progressBar2, progressBar3; private ImageView img1, img2, img3; private static final String IMG_URI = "http://www.dbjr.com.cn/images/logo.gif"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } /** * 初始化監(jiān)聽器 */ private void initListener() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } /** * * 初始化控件 */ private void initView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); progressBar1 = (ProgressBar) findViewById(R.id.progressbar1); progressBar2 = (ProgressBar) findViewById(R.id.progressbar2); progressBar3 = (ProgressBar) findViewById(R.id.progressbar3); img1 = (ImageView) findViewById(R.id.img1); img2 = (ImageView) findViewById(R.id.img2); img3 = (ImageView) findViewById(R.id.img3); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { // 點(diǎn)擊按鈕模擬下載 MyDownloadAsy down = new MyDownloadAsy(); down.execute(IMG_URI, arg0.getId() + ""); } /** * 1.Params,傳遞給后臺(tái)任務(wù)的參數(shù)類型。 * * 2.Progress,后臺(tái)計(jì)算執(zhí)行過程中,進(jìn)步單位(progress units)的類型。(就是后臺(tái)程序已經(jīng)執(zhí)行了百分之幾了。) * * 3.Result, 后臺(tái)執(zhí)行返回的結(jié)果的類型。 */ class MyDownloadAsy extends AsyncTask<String, Integer, Bitmap> { private static final String TAG = "asy"; private int clickBtn = 0; private void i(String object) { Log.i(TAG, object); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); i("準(zhǔn)備運(yùn)行線程"); progressBar1.setProgress(0);// 進(jìn)度條復(fù)位 progressBar2.setProgress(0);// 進(jìn)度條復(fù)位 progressBar3.setProgress(0);// 進(jìn)度條復(fù)位 } @Override protected Bitmap doInBackground(String... arg0) { switch (Integer.parseInt(arg0[1])) { case R.id.btn1: clickBtn = 1; break; case R.id.btn2: clickBtn = 2; break; case R.id.btn3: clickBtn = 3; break; default: break; } i("正在后臺(tái)執(zhí)行"); publishProgress(0); // 下載圖片 HttpClient hc = new DefaultHttpClient(); // 等待2s sleepWait(); publishProgress(50); HttpGet hg = new HttpGet(arg0[0]);// 獲取jb51的logo final Bitmap bm; try { HttpResponse hr = hc.execute(hg); bm = BitmapFactory.decodeStream(hr.getEntity().getContent()); } catch (Exception e) { return null; } sleepWait(); publishProgress(100); // mImageView.setImageBitmap(result); 不能在后臺(tái)線程操作ui return bm; } /** * 等待2s鐘 */ private void sleepWait() { try { Thread.sleep(2000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } protected void onProgressUpdate(Integer... values) { // 動(dòng)態(tài)更新 i("進(jìn)度更新"); switch (clickBtn) { case 1: progressBar1.setProgress(values[0]);// 更新進(jìn)度條的進(jìn)度 break; case 2: progressBar2.setProgress(values[0]);// 更新進(jìn)度條的進(jìn)度 break; case 3: progressBar3.setProgress(values[0]);// 更新進(jìn)度條的進(jìn)度 break; default: break; } } protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); i("線程執(zhí)行完成"); if (result != null) { i("下載圖片成功"); switch (clickBtn) { case 1: img1.setImageBitmap(result); break; case 2: img2.setImageBitmap(result); break; case 3: img3.setImageBitmap(result); break; default: break; } } else { i("下載圖片失敗"); } } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); i("取消線程"); switch (clickBtn) { case 1: progressBar1.setProgress(0);// 進(jìn)度條復(fù)位 break; case 2: progressBar2.setProgress(0);// 進(jìn)度條復(fù)位 break; case 3: progressBar3.setProgress(0);// 進(jìn)度條復(fù)位 break; default: break; } } } }
xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" > <TableRow> <Button android:id="@+id/btn1" android:text="@string/btn1" /> <ProgressBar android:id="@+id/progressbar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img1" android:src="@drawable/ic_launcher" /> </TableRow> <TableRow> <Button android:id="@+id/btn2" android:text="@string/btn2" /> <ProgressBar android:id="@+id/progressbar2" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img2" android:src="@drawable/ic_launcher" /> </TableRow> <TableRow> <Button android:id="@+id/btn3" android:text="@string/btn3" /> <ProgressBar android:id="@+id/progressbar3" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img3" android:src="@drawable/ic_launcher" /> </TableRow> </TableLayout> </RelativeLayout>
AndroidManifast:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xunfang.asynctackdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <!-- 訪問網(wǎng)絡(luò)的權(quán)限 --> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.xunfang.asynctackdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見問題解決方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android教程之使用asynctask在后臺(tái)運(yùn)行耗時(shí)任務(wù)
- Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
- Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一)
- Android中使用AsyncTask實(shí)現(xiàn)文件下載以及進(jìn)度更新提示
- Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
- Android中使用AsyncTask實(shí)現(xiàn)下載文件動(dòng)態(tài)更新進(jìn)度條功能
- Android使用AsyncTask下載圖片并顯示進(jìn)度條功能
- 詳解Android中AsyncTask的使用方法
- Android AsyncTask詳解及使用方法
- Android中AsyncTask的入門使用學(xué)習(xí)指南
相關(guān)文章
Kotlin開發(fā)實(shí)戰(zhàn)之hello world
這篇文章主要為大家詳細(xì)介紹了Kotlin開發(fā)實(shí)戰(zhàn)之hello world的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05學(xué)習(xí)使用Material Design控件(四)Android實(shí)現(xiàn)標(biāo)題欄自動(dòng)縮放、放大效果
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,Android實(shí)現(xiàn)標(biāo)題欄自動(dòng)縮放、放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07深入理解Android熱修復(fù)技術(shù)原理之資源熱修復(fù)技術(shù)
Android資源的熱修復(fù),就是在app不重新安裝的情況下,利用下發(fā)的補(bǔ)丁包 直接更新本app中的資源2021-06-06Android編程實(shí)現(xiàn)變化的雙重選擇框功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)變化的雙重選擇框功能,結(jié)合實(shí)例形式分析了Android雙重選擇框功能的樣式布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10android實(shí)現(xiàn)ViewPager的Indicator的實(shí)例代碼
本篇文章主要介紹了android實(shí)現(xiàn)ViewPager的Indicator的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android自定義View實(shí)現(xiàn)仿網(wǎng)易音樂唱片播放效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)仿網(wǎng)易音樂唱片播放效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04