import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Handler handler = new Handler(Looper.getMainLooper());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// Background Thread
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000); // Simulate long task (3 sec delay)
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update UI using Handler
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("Text Updated After 3 Seconds!");
}
});
}
}).start();
}
}
Handler & Runnable in hindi (Development of Android applications)
Handler in Hindi
- Handler Android में एक महत्वपूर्ण class है, जो threads के बीच communication और message handling के लिए उपयोग की जाती है।
- यह message queue और looper के साथ काम करता है, जिससे background threads से main (UI) thread को update किया जा सकता है।
- Handler का उपयोग delayed execution, periodic tasks और asynchronous message processing के लिए किया जाता है।
- यह sendMessage() और post() methods की मदद से tasks को queue में डालता है और उन्हें execute करता है।
- Looper continuously MessageQueue को check करता है और available messages को Handler की मदद से execute करता है।
- HandlerThread एक background thread होता है, जो internally looper को manage करता है और long-running background tasks को efficiently execute करता है।
- यदि कोई background thread सीधे UI thread को update करने की कोशिश करता है, तो android.view.ViewRootImpl$CalledFromWrongThreadException आ सकती है।
- runOnUiThread(), View.post(), और Handler.post() जैसी techniques UI thread को safely update करने के लिए उपयोग की जाती हैं।
- WeakHandler memory leaks को रोकने के लिए इस्तेमाल किया जाता है, ताकि Handler activity या fragment के destroy होने के बाद भी retain न हो।
- WorkManager, LiveData, और Coroutines जैसी modern techniques अब background tasks को handle करने के लिए preferred मानी जाती हैं, लेकिन Handler अब भी lightweight और efficient communication के लिए उपयोग किया जाता है।
Basic Example of Handler in Android (Java)
Code Explanation:
- Handler को Looper.getMainLooper() के साथ initialize किया गया, जिससे यह main thread (UI thread) के साथ काम कर सके।
- एक background thread (new Thread()) बनाया, जिसमें 3 सेकंड का delay दिया गया (Thread.sleep(3000))।
- Handler.post() का उपयोग करके UI को background thread से update किया, क्योंकि background thread सीधे UI को update नहीं कर सकता।
Output:
जब app launch होगी, तो 3 सेकंड बाद TextView का text "Text Updated After 3 Seconds!" में बदल जाएगा।
Runnable in Hindi
- Runnable एक functional interface है, जिसका उपयोग threads में background tasks को execute करने के लिए किया जाता है।
- यह java.lang.Runnable interface का हिस्सा है और इसे Thread या ExecutorService के साथ उपयोग किया जाता है।
- Runnable को implement करने के लिए run() method को override करना पड़ता है, जिसमें वह code लिखा जाता है जो background में execute होगा।
- इसे Thread.start() के साथ use किया जाता है, जिससे नया thread create होकर background में execute होता है।
- Anonymous Runnable का उपयोग lambda expressions के साथ करके code को short और readable बनाया जा सकता है।
- Handler.post(Runnable) का उपयोग करके UI thread को safely update किया जा सकता है।
- Executors.newSingleThreadExecutor() का उपयोग Runnable को manage करने और multiple tasks को sequentially execute करने के लिए किया जाता है।
- ScheduledExecutorService का उपयोग Runnable tasks को delay या periodic execution के लिए किया जाता है।
- runOnUiThread() का उपयोग Runnable को main thread पर execute करने के लिए किया जाता है, जिससे UI update किया जा सके।
- अब Kotlin Coroutines को background task execution के लिए preferred माना जाता है, लेकिन Runnable अब भी lightweight और effective solution है।
नीचे Runnable के उपयोग के कुछ उदाहरण दिए गए हैं:
1. Basic Runnable Example
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Background task is running...");
}
}
// इसे इस तरह use करें:
Thread thread = new Thread(new MyRunnable());
thread.start();
Code Explanation:
- MyRunnable class Runnable interface को implement करती है।
- Thread.start() method इसे नए thread में execute करता है।
2. Anonymous Runnable Example
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Running in background...");
}
});
thread.start();
Code Explanation:
- Anonymous class का उपयोग करके Runnable को बिना अलग class बनाए ही execute किया गया।
3. Runnable with Lambda Expression (Java 8+)
Thread thread = new Thread(() -> {
System.out.println("Lambda Runnable is running...");
});
thread.start();
Code Explanation:
- Lambda expression का उपयोग करके code को छोटा और readable बनाया गया।
Request
अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!
ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।
आपके support और understanding के लिए दिल से धन्यवाद।