एंड्रॉइड में थ्रेड से टोस्ट संदेश प्रदर्शित करना
यह उदाहरण दर्शाता है कि एंड्रॉइड में कोटलिन का उपयोग करके बैकग्राउंड थ्रेड से टोस्ट संदेश कैसे दिखाया जाए। चूंकि टोस्ट UI थ्रेड पर ही दिखाए जा सकते हैं, हमें runOnUiThread या Handler का उपयोग करना होगा।
चरण 1: नया प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में File → New Project पर जाएं और आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: MainActivity.kt में कोड जोड़ें
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var toast: Toast
private lateinit var thread: Thread
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
// टोस्ट ऑब्जेक्ट बनाएं
toast = Toast.makeText(this, "यह थ्रेड से एक टोस्ट है", Toast.LENGTH_SHORT)
// बैकग्राउंड थ्रेड बनाएं
thread = Thread(Runnable {
for (i in 0..9) { // डेमो के लिए 10 बार लूप
try {
Thread.sleep(2000) // 2 सेकंड प्रतीक्षा करें
// UI थ्रेड पर टोस्ट दिखाएं
runOnUiThread {
toast.show()
}
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
})
thread.start()
}
override fun onDestroy() {
super.onDestroy()
thread.interrupt() // थ्रेड को साफ करें
}
}
महत्वपूर्ण बिंदु:
- runOnUiThread: यह सुनिश्चित करता है कि टोस्ट मुख्य UI थ्रेड पर दिखाया जाए
- Thread.sleep: बैकग्राउंड थ्रेड को रोकने के लिए उपयोग किया जाता है
- onDestroy में थ्रेड इंटरप्ट: मेमोरी लीक से बचने के लिए एक्टिविटी नष्ट होने पर थ्रेड रोकें
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.toastfromthread">
<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>
बेहतर दृष्टिकोण: Handler का उपयोग
आधुनिक एंड्रॉइड विकास के लिए, Handler या Coroutines का उपयोग करना अधिक बेहतर है:
// Handler दृष्टिकोण
val handler = Handler(Looper.getMainLooper())
thread = Thread {
for (i in 0..9) {
Thread.sleep(2000)
handler.post { toast.show() }
}
}
// कोटलिन कोरूटीन दृष्टिकोण (अनुशंसित)
lifecycleScope.launch {
repeat(10) {
delay(2000)
withContext(Dispatchers.Main) {
toast.show()
}
}
}