यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android सर्विस को हमेशा बैकग्राउंड में कैसे चलाया जाता है। START_STICKY और onTaskRemoved का उपयोग करके सर्विस को पुनः आरंभ किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में UI जोड़ें
<?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_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here to start background Service"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: बैकग्राउंड सर्विस बनाएं (MyService.kt)
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.widget.Toast
class MyService : Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
onTaskRemoved(intent)
Toast.makeText(
applicationContext,
"This is a Service running in Background",
Toast.LENGTH_SHORT
).show()
return START_STICKY
}
override fun onBind(intent: Intent): IBinder? {
throw UnsupportedOperationException("Not yet implemented")
}
override fun onTaskRemoved(rootIntent: Intent) {
val restartServiceIntent = Intent(applicationContext, this.javaClass)
restartServiceIntent.setPackage(packageName)
startService(restartServiceIntent)
super.onTaskRemoved(rootIntent)
}
}
चरण 4: MainActivity.kt में सर्विस शुरू करें
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val button: Button = findViewById(R.id.button)
button.setOnClickListener { startService(Intent(applicationContext, MyService::class.java)) }
}
}
चरण 5: AndroidManifest.xml में सर्विस घोषित करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।