কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android সার্ভিস পটভূমিতে সর্বদা চালানোর পদ্ধতি


এই উদাহরণটি দেখায় কিভাবে Kotlin ব্যবহার করে একটি Android সার্ভিসকে পটভূমিতে সর্বদা চালিয়ে রাখা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: activity_main.xml ডিজাইন করুন

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"
    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>

ধাপ ৩: সার্ভিস ক্লাস তৈরি করুন (MyService.kt)

একটি নতুন Kotlin ক্লাস 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)
    }
}

ধাপ ৪: MainActivity.kt-এ সার্ভিস শুরু করার কোড লিখুন

src/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)) }
    }
}

ধাপ ৫: AndroidManifest.xml-এ সার্ভিস ডিক্লেয়ার করুন

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 Studio থেকে Run করুন। বাটনে ক্লিক করলে পটভূমি সার্ভিস শুরু হবে।

  1. Android-এ ব্যাকগ্রাউন্ড সার্ভিস তৈরি করার পূর্ণাঙ্গ গাইড

  2. অ্যান্ড্রয়েডে ব্যাকগ্রাউন্ড সার্ভিস সবসময় চালানোর পদ্ধতি

  3. Kotlin ব্যবহার করে Android সার্ভিস থেকে নোটিফিকেশন পাঠানো

  4. উইন্ডোজ ১১-এ WSA সবসময় ব্যাকগ্রাউন্ডে রান করার পদ্ধতি