deepeweb.github.io

Handler & Runnable in hindi (Development of Android applications)

Handler in Hindi

Basic Example of Handler in Android (Java)

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();
    }
}

Code Explanation:

Output:

जब app launch होगी, तो 3 सेकंड बाद TextView का text "Text Updated After 3 Seconds!" में बदल जाएगा।

Runnable in Hindi

नीचे 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:

2. Anonymous Runnable Example

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("Running in background...");
    }
});
thread.start();

Code Explanation:

3. Runnable with Lambda Expression (Java 8+)

Thread thread = new Thread(() -> {
    System.out.println("Lambda Runnable is running...");
});
thread.start();

Code Explanation:




Request

अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!

ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।

आपके support और understanding के लिए दिल से धन्यवाद।





Ad
Ad


Ad
Ad

Follow Us

Facebook Logo    Instagram Logo