ก่อนเข้าสู่ตัวอย่างเราควรทราบว่า asyncTask คืออะไร AsyncTask จะดำเนินการ / ดำเนินการในเธรดพื้นหลังและอัปเดตบนเธรดหลัก นี่คือวิธีแก้ปัญหาง่ายๆ เกี่ยวกับ Android AsyncTask Parallel Execution
ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่
ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml .
<?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:id = "@+id/rootview" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:background = "#c1c1c1" android:gravity = "center_horizontal" tools:context = ".MainActivity"> <Button android:id = "@+id/asyncTask" android:text = "Download" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> <ImageView android:id = "@+id/image" android:layout_width = "300dp" android:layout_height = "300dp" /> <ImageView android:id = "@+id/image2" android:layout_width = "300dp" android:layout_height = "300dp" /> </LinearLayout>
ในโค้ดด้านบนนี้ เราได้ประกาศการดูรูปภาพสองภาพและหนึ่งปุ่ม เมื่อผู้ใช้คลิกที่ปุ่ม ระบบจะดาวน์โหลดรูปภาพสองภาพจากแหล่งอินเทอร์เน็ตที่แตกต่างกันและผนวกเข้ากับการดูรูปภาพ
ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java
package com.example.andy.myapplication;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
URL ImageUrl = null;
InputStream is = null;
Bitmap bmImg = null;
ImageView imageView = null;
ImageView imageView2 = null;
AsyncTaskExample asyncTask = null;
AsyncTaskExample2 asyncTask2 = null;
ProgressDialog p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.asyncTask);
imageView = findViewById(R.id.image);
imageView2 = findViewById(R.id.image2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
asyncTask2 = new AsyncTaskExample2();
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "https://www.tutorialspoint.com/cprogramming/images/logo.png");
asyncTask = new AsyncTaskExample();
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "https://www.tutorialspoint.com/images/tp-logo-diamond.png");
}
});
}
private class AsyncTaskExample extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
p = new ProgressDialog(MainActivity.this);
p.setMessage("Please wait...It is downloading");
p.setIndeterminate(true);
p.setCancelable(false);
p.show();
}
@Override
protected Bitmap doInBackground(String... strings) {
try {
ImageUrl = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) ImageUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
bmImg = BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (imageView ! = null) {
p.hide();
imageView.setImageBitmap(bitmap);
} else {
p.show();
}
}
}
private class AsyncTaskExample2 extends AsyncTask<String, String, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
try {
ImageUrl = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) ImageUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
bmImg = BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (imageView2 ! = null) {
imageView2.setImageBitmap(bitmap);
} else {
}
}
}
} ในโค้ดด้านบน เราได้ให้ excuteOnExcutor() เพื่อรัน aynctask สองอันขึ้นไป Android จะรองรับการทำงานแบบคู่ขนานสูงสุด 5 รายการ
ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน manifest.xml
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <uses-permission android:name = "android.permission.INTERNET"/> <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"> <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>
ในโค้ดด้านบนนี้ เราให้สิทธิ์อินเทอร์เน็ตในการดาวน์โหลดรูปภาพจากแหล่งอินเทอร์เน็ต
มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์ของคุณแล้วคลิก Run Eclipse Run
ไอคอนจากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

เมื่อผู้ใช้คลิกที่ปุ่ม ระบบจะดาวน์โหลดรูปภาพจากแหล่งอินเทอร์เน็ตโดยใช้แถบความคืบหน้าดังแสดงด้านล่าง -

มันจะดาวน์โหลดภาพสองภาพแบบขนานและแสดงผลดังที่แสดงด้านล่าง -
