परिचय
यह ट्यूटोरियल दिखाता है कि AsyncTask के लिए टाइमआउट कैसे सेट करें। ध्यान दें: AsyncTask API 30 (Android 11) में डिप्रिकेटेड है। आधुनिक ऐप्स के लिए Coroutines, RxJava, या ExecutorService उपयोग करें। यह उदाहरण केवल लिगेसी कोड समझने के लिए है।
आवश्यक चरण
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:layout_marginTop="50dp"
android:id="@+id/enterTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@android:drawable/editbox_background"
android:inputType="number"
android:hint="सेकंड में समय दर्ज करें" />
<Button
android:id="@+id/btnRun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AsyncTask चलाएं"
android:layout_below="@id/enterTime"
android:layout_centerHorizontal="true" />
</RelativeLayout>
चरण 3: मुख्य गतिविधि (src/MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editTextTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btnRun);
editTextTime = findViewById(R.id.enterTime);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner runner = new AsyncTaskRunner();
String sleepTime = editTextTime.getText().toString();
runner.execute(sleepTime);
}
});
}
// इनर क्लास AsyncTask
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
publishProgress("AsyncTask शुरू हुआ...");
String resp;
try {
int time = Integer.parseInt(params[0]) * 1000; // सेकंड को मिलीसेकंड में
Thread.sleep(time); // सिम्युलेटेड लॉन्ग टास्क
resp = "कुल " + params[0] + " सेकंड तक सोया";
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "AsyncTask समाप्त: " + result, Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this, "प्रोग्रेस डायलॉग",
"प्रतीक्षा करें " + editTextTime.getText().toString() + " सेकंड");
}
@Override
protected void onProgressUpdate(String... text) {
Toast.makeText(MainActivity.this, text[0], Toast.LENGTH_SHORT).show();
}
}
}
चरण 4: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run क्लिक करें। सेकंड दर्ज करें (जैसे 5), "AsyncTask चलाएं" दबाएं - प्रोग्रेस डायलॉग दिखेगा और निर्दिष्ट समय बाद बंद होगा।

⚠️ AsyncTask टाइमआउट की सीमाएं और आधुनिक विकल्प
उपरोक्त कोड में बिल्ट-इन टाइमआउट नहीं है - यह केवल यूजर इनपुट के आधार पर स्लीप करता है। वास्तविक टाइमआउट के लिए:
विकल्प 1: ExecutorService + Future (Java मानक)
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
// लंबा टास्क
return "परिणाम";
});
try {
String result = future.get(10, TimeUnit.SECONDS); // 10 सेकंड टाइमआउट
} catch (TimeoutException e) {
future.cancel(true); // टास्क कैंसल करें
}
विकल्प 2: Kotlin Coroutines (अनुशंसित)
lifecycleScope.launch {
try {
val result = withTimeout(10_000) { // 10 सेकंड
// सस्पेंड फंक्शन कॉल
repository.getData()
}
} catch (e: TimeoutCancellationException) {
// टाइमआउट हैंडल करें
}
}
विकल्प 3: RxJava
Observable.fromCallable(() -> longRunningTask())
.timeout(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {}, error -> {});
सारांश
- AsyncTask डिप्रिकेटेड है - नए प्रोजेक्ट्स में उपयोग न करें।
- बिल्ट-इन टाइमआउट नहीं है - मैन्युअल इम्प्लीमेंटेशन चाहिए।
- Coroutines + withTimeout आधुनिक, सरल और सुरक्षित तरीका है।
- हमेशा कैंसलेशन सही से हैंडल करें (रिसोर्स लीक रोकने के लिए)।