उदाहरण में जाने से पहले, हमें पता होना चाहिए कि Android में AsyncTask क्या है। AsyncTask बैकग्राउंड थ्रेड में ऑपरेशन/एक्शन करता है और मेन थ्रेड पर अपडेट करता है। बैकग्राउंड थ्रेड पर ऑपरेशन करते समय, उपयोगकर्ता निम्नलिखित कोड का उपयोग करके ऑपरेशन कैंसिल कर सकता है:
AsyncTaskExample mAsyncTask = new AsyncTaskExample(); mAsyncTask.cancel(true);
AsyncTask कैंसिल करने का उदाहरण
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और आवश्यक विवरण भरें।
चरण 2 - लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://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" />
</LinearLayout>
उपरोक्त कोड में, जब आप बटन पर क्लिक करते हैं, तो यह इंटरनेट स्रोत से इमेज डाउनलोड करेगा और ImageView में जोड़ देगा।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private AsyncTaskExample asyncTask;
private ProgressDialog progressDialog;
@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);
button.setOnClickListener(v -> {
asyncTask = new AsyncTaskExample();
asyncTask.execute("https://www.tutorialspoint.com/images/tp-logo-diamond.png");
});
}
private class AsyncTaskExample extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait... Downloading");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(dialog -> {
asyncTask.cancel(true);
Toast.makeText(MainActivity.this, "AsyncTask stopped", Toast.LENGTH_LONG).show();
});
progressDialog.show();
}
@Override
protected Bitmap doInBackground(String... strings) {
try {
Thread.sleep(10000); // डेमो के लिए देरी
URL imageUrl = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (bitmap != null && imageView != null && !isCancelled()) {
imageView.setImageBitmap(bitmap);
}
}
}
}
चरण 4 - Manifest में परमिशन जोड़ें
AndroidManifest.xml में इंटरनेट परमिशन जोड़ें:
<uses-permission android:name="android.permission.INTERNET"/>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। डाउनलोड बटन पर क्लिक करने पर प्रोग्रेस बार शुरू होगा। प्रोग्रेस बार के बाहर कहीं भी क्लिक करने पर डायलॉग कैंसिल होगा और AsyncTask रुक जाएगा।
महत्वपूर्ण: Android 11 (API 30) से AsyncTask डिप्रिकेटेड है। आधुनिक ऐप्स के लिए
Coroutines(Kotlin),RxJava, याExecutorServiceका उपयोग करने की सिफारिश की जाती है।