說實話,筆者是菜鳥,之前有做過一次獲取利用AsyncTask獲取源代碼的實驗,但是不成功,今天剛好有興趣,看了一下《瘋狂Android講義》對應AsyncTask的知識點,瞭解了AsyncTask類的基本用法,琢磨了一下,終於成功,所以來這裏分享一下,也希望可以幫到有需要的小夥伴,下面是具體代碼
MainActivity.java
package poison.project.asynctaskexperiment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends Activity{
private TextView show; //定義需要獲取的組件
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show=(TextView)findViewById(R.id.show);
btn=(Button)findViewById(R.id.button);
//爲按鈕綁定事件響應方法
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
try {
download();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
//重寫該方法,爲界面的按鈕提供事件響應方法
public void download() throws MalformedURLException{
CodeTask task=new CodeTask(this);
task.execute(new URL("https://www.baidu.com/"));
}
class CodeTask extends AsyncTask<URL,Integer,String>{
//可變長的輸入參數,與AsyncTask.exucute()對應
ProgressDialog progressDialog;
//定義記錄已讀取行的數量
int hasRead=0;
Context mContext;
public CodeTask(Context ctx){
mContext=ctx;
}
//完成實際的下載任務
protected String doInBackground(URL... params){
StringBuilder sb=new StringBuilder();
try{
URLConnection conn=params[0].openConnection();
//打開conn連接對應的輸入流,並把它包裝成BufferedReader
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
String line=null;
while((line=br.readLine())!=null){
sb.append(line+"\n");
hasRead++; //記錄行數
publishProgress(hasRead); //更新任務的執行進度
}
return sb.toString();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
//返回HTML頁面內容
show.setText(result);
progressDialog.dismiss();
}
//負責在下載的時候顯示一個進度條
@Override
protected void onPreExecute(){
progressDialog=new ProgressDialog(mContext);
//設置對話框的標題
progressDialog.setTitle("任務正在執行中...");
//設置對話框顯示的內容
progressDialog.setMessage("任務正在執行中,請等待...");
//設置對話框不能用“取消”按鈕關閉
progressDialog.setCancelable(false);
//設置該進度條的最大進度值
progressDialog.setMax(202);
//設置對話框的進度條風格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置對話框的進度條是否顯示進度
progressDialog.setIndeterminate(false);
progressDialog.show();
}
//負責在下載的過程更新下載進度的進度條值
@Override
protected void onProgressUpdate(Integer... values){
//更新進度
show.setText("已經讀取了【"+values[0]+"】行!");
progressDialog.setProgress(values[0]);
}
}
}
上面程序其實很容易理解,它只是按照了AsyncTask一般使用步驟,具體不瞭解的可百度AsyncTask的用法,都有詳細的介紹
該程序的重點是實現AsyncTask的子類,實現該該子類時實現瞭如下四個方法:
doInBackground():該方法完成實際的下載任務,返回一個結果給onPostExecute()方法
onPreExecute():該方法負責在下載開始的時候顯示一個進度條
onProgressUppdate():該方法負責更新下載進度的進度條的值
onPostExecute():該方法負責在下載完成後,將下載代碼顯示出來
由於該程序使用了網絡編程從網絡上下載數據,需要訪問網絡,所以要在AndroidMainfest.xml文件中聲明以下權限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
如果讀者安裝的是Android9.0的話,還需要再<application... />中加上
android:usesCleartextTraffic="true"
筆者用的就是Android9.0,所以在此特別提醒,希望小夥伴們可以避開我犯過的錯
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="poison.project.asynctaskexperiment" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
佈局文件,這部分比較簡單,在這裏就不做講解了
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginTop="20dp"
android:text="下載"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/show" />
</android.support.constraint.ConstraintLayout>
筆者水平有限,只能憑己所學做粗略說明,本程序親測有效,截圖如下: