欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android通過(guò)startService實(shí)現(xiàn)文件批量下載

 更新時(shí)間:2021年09月08日 08:44:34   作者:孫群  
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)startService實(shí)現(xiàn)文件批量下載的示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

關(guān)于startService的基本使用概述及其生命周期可參見(jiàn)《Android中startService基本使用方法概述》。

本文通過(guò)批量下載文件的簡(jiǎn)單示例,演示startService以及stopService(startId)的使用流程,具體內(nèi)容如下

系統(tǒng)界面如下:

界面很簡(jiǎn)單,就一個(gè)按鈕“批量下載文章”,通過(guò)該Activity上的按鈕啟動(dòng)DownloadService。

DownloadService是用來(lái)進(jìn)行下載CSDN上博客文章的服務(wù),代碼如下:

package com.ispring.startservicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class DownloadService extends Service {
  //存儲(chǔ)所有的startId
  private List<Integer> allStartIdList = new ArrayList<>();
  //存儲(chǔ)已經(jīng)下載完成的startId
  private List<Integer> finishedStartIdList = new ArrayList<>();

  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      if(msg.what == 1){
        String tip = (String)msg.obj;
        Toast.makeText(DownloadService.this, tip, Toast.LENGTH_LONG).show();
      }
    }
  };

  class DownloadThread extends Thread {

    //對(duì)應(yīng)的intent的startId信息
    private int startId = 0;

    //要下載的文章名稱(chēng)
    private String blogName = null;

    //要下載的文章地址
    private String blogUrl = null;

    public DownloadThread(int startId, String name, String url){
      this.startId = startId;
      this.blogName = name;
      this.blogUrl = url;
    }

    @Override
    public void run() {
      HttpURLConnection conn = null;
      InputStream is = null;
      try{
        //下載指定的文件
        URL url = new URL(this.blogUrl);
        conn = (HttpURLConnection)url.openConnection();
        if(conn != null){
          //我們?cè)诖颂幍玫剿螺d文章的輸入流,可以將其以文件的形式寫(xiě)入到存儲(chǔ)卡上面或
          //將其讀取出文本顯示在App中
          is = conn.getInputStream();
        }
      }catch (MalformedURLException e){
        e.printStackTrace();
      }catch (IOException e){
        e.printStackTrace();
      }finally {
        if(conn != null){
          conn.disconnect();
        }
      }

      finishedStartIdList.add(startId);

      if(finishedStartIdList.containsAll(allStartIdList)){
        String tip = "全部下載完成, 數(shù)量" + finishedStartIdList.size();
        Message msg = handler.obtainMessage(1);
        msg.obj = tip;
        handler.sendMessage(msg);
      }

      Log.i("DemoLog", "stopSelf(" + startId + ")");
      stopSelf(startId);
    }
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.i("DemoLog", "DownloadService -> onCreate");
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    allStartIdList.add(startId);
    String name = intent.getStringExtra("name");
    String url = intent.getStringExtra("url");
    Log.i("DemoLog", "DownloadService -> onStartCommand, startId: " + startId + ", name: " + name);
    DownloadThread downloadThread = new DownloadThread(startId, name, url);
    downloadThread.start();
    return START_REDELIVER_INTENT;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.i("DemoLog", "DownloadService -> onDestroy");
  }
}

DownloadActivity的代碼如下:

package com.ispring.startservicedemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class DownloadActivity extends Activity implements Button.OnClickListener {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download);
  }

  @Override
  public void onClick(View v) {
    List<String> list = new ArrayList<>();
    list.add("Android中startService基本使用方法概述;//www.dbjr.com.cn/article/76470.htm");
    list.add("Android登陸界面實(shí)現(xiàn)清除輸入框內(nèi)容和震動(dòng)效果;//www.dbjr.com.cn/article/76328.htm");

    Iterator iterator = list.iterator();

    while (iterator.hasNext()){
      String str = (String)iterator.next();
      String[] splits = str.split(";");
      String name = splits[0];
      String url = splits[1];
      Intent intent = new Intent(this, DownloadService.class);
      intent.putExtra("name", name);
      intent.putExtra("url", url);
      startService(intent);
    }
  }
}

當(dāng)我們單擊了按鈕“批量下載文章”時(shí),我們會(huì)多次調(diào)用Activity的startService方法,其中我們?cè)谄鋮?shù)intent中存儲(chǔ)了文章名name以及文章的地址url,由于我們多次調(diào)用了startService方法,所以會(huì)批量下載文章。

點(diǎn)擊按鈕后,控制臺(tái)運(yùn)行結(jié)果如下所示:

調(diào)用了startService之后,Android Framework接收到了intent信息,第一次會(huì)先創(chuàng)建DownloadService的實(shí)例,然后執(zhí)行其onCreate回調(diào)方法,onCreate在Service的生命周期中只會(huì)調(diào)用一次。

調(diào)用了onCreate方法后,Android會(huì)自動(dòng)回調(diào)其onStartCommand方法,其實(shí)每次調(diào)用Context的startService都會(huì)觸發(fā)onStartCommand回調(diào)方法,所以onStartCommand在Service的生命周期中可能會(huì)被調(diào)用多次。在onStartCommand方法中我們可以獲得intent和startId,intent即我們調(diào)用startService方法時(shí)傳入的參數(shù),startId是Android自動(dòng)分配的,每次調(diào)用startService都會(huì)自動(dòng)得到一個(gè)startId,一個(gè)startId就意味著一個(gè)job,也就是意味著一次下載任務(wù)。我們?cè)贒ownloadService中有兩個(gè)字段allStartIdList和finishedStartIdList。allStartIdList存儲(chǔ)著所有的startId,我們?cè)趏nStartCommand方法一開(kāi)始就把我們得到的startId放入到allStartIdList中存儲(chǔ),然后我們根據(jù)intent讀取到文章名name和文章地址url,并根據(jù)這些信息創(chuàng)建一個(gè)新的線程DownloadThread,該線程用于執(zhí)行實(shí)際的網(wǎng)絡(luò)請(qǐng)求下載工作。需要注意的是,為了代碼簡(jiǎn)化起見(jiàn)我們?cè)趏nStartCommand中只要得到intent就開(kāi)辟一個(gè)新線程(DownloadThread),但是在實(shí)際生產(chǎn)環(huán)境中這樣的開(kāi)銷(xiāo)比較大(線程新建、線程銷(xiāo)毀),應(yīng)該盡量使用線程池以節(jié)約開(kāi)銷(xiāo)。

執(zhí)行了DownloadThread的start方法后,就會(huì)執(zhí)行DownloadThread線程的run方法,在該方法中我們會(huì)執(zhí)行網(wǎng)絡(luò)請(qǐng)求,獲取博客文章的輸入流,當(dāng)我們獲取到該輸入流之后,我們就認(rèn)為下載完成了,此時(shí)我們可以將其以文件的形式寫(xiě)入到存儲(chǔ)卡上,也可以將其讀取出文本顯示在App上,此處我們沒(méi)有對(duì)輸入流做任何處理,我們就認(rèn)為下載完成了。下載完成后,我們把startId存入到finishedStartIdList中,finishedStartIdList存儲(chǔ)著所有已經(jīng)完成的job的startId。當(dāng)finishedStartIdList中已經(jīng)包含了allStartIdList的所有startId時(shí),說(shuō)明我們所有的下載任務(wù)完成了,我們會(huì)通過(guò)handler讓主線程顯示Toast告知用戶文章下載完成。在run方法的最后我們會(huì)執(zhí)行Service的stopSelf(startId)方法。需要注意的是我們?cè)趕topSelf方法中傳入了startId,這意味著我們不是直接停止Service的運(yùn)行,我們只是停止該startId對(duì)應(yīng)的job的執(zhí)行,如果所有的startId所對(duì)應(yīng)的job都停止了,那么整個(gè)Service才會(huì)停止,當(dāng)整個(gè)Service停止時(shí),才會(huì)觸發(fā)執(zhí)行Service的onDestroy回調(diào)方法。

本文只是通過(guò)批量下載博文這一簡(jiǎn)單示例演示通過(guò)startService以及stopSelf(startId)使用Service基本使用流程,代碼沒(méi)有進(jìn)行優(yōu)化,希望對(duì)大家學(xué)習(xí)Service有所幫助。

相關(guān)文章

最新評(píng)論