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

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

সহজ ভাষায় Android Background Service তৈরি করা

এই টিউটোরিয়ালে একটি মৌলিক ব্যাকগ্রাউন্ড সার্ভিস তৈরি করার ধাপে ধাপে পদ্ধতি দেখানো হলো। এটি UI থ্রেড ছাড়া স্বতন্ত্রভাবে চলতে পারে এবং এক্টিভিটি বন্ধ থাকলেও কাজ করতে পারে।

ধাপ ১: প্রজেক্ট সেটআপ

Android Studio-তে নতুন "Empty Activity" প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ধাপ ৩: MainActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView text = findViewById(R.id.text);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("Started".equals(text.getText().toString())) {
                    text.setText("Stopped");
                    stopService(new Intent(MainActivity.this, SimpleService.class));
                } else {
                    text.setText("Started");
                    startService(new Intent(MainActivity.this, SimpleService.class));
                }
            }
        });
    }
}

ধাপ ৪: SimpleService.java

package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class SimpleService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null; // বাউন্ড সার্ভিস না
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service started by user", Toast.LENGTH_LONG).show();
        // এখানে আপনার ব্যাকগ্রাউন্ড লজিক লিখুন
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
    }
}

ধাপ ৫: AndroidManifest.xml

<service android:name=".SimpleService" />

সার্ভিস টাইপসমূহ

  • Started Service: startService() দিয়ে চালু, stopService() দিয়ে বন্ধ (এটি)
  • Bound Service: bindService() দিয়ে বাইন্ড, ইন্টারফেসের মাধ্যমে কমিউনিকেশন
  • Foreground Service: নোটিফিকেশন সহ, ইউজার দেখতে পায়
  • IntentService: ব্যাকগ্রাউন্ড থ্রেডে সিকুয়েনশিয়াল টাস্ক হ্যান্ডেল করে (Deprecated API 30+)

সার্ভিস লাইফসাইকেল

  1. onCreate() - একবার ইনিশিয়ালাইজেশন
  2. onStartCommand() - প্রতিবার startService() কল করলে
  3. onBind() - bindService() কল করলে (যদি বাউন্ড হয়)
  4. onDestroy() - সার্ভিস বন্ধ হলে ক্লিনআপ

সর্বোত্তম অভ্যাস

  • দীর্ঘস্থায়ী কাজের জন্য আলাদা থ্রেড বা ExecutorService ব্যবহার করুন
  • onDestroy-এ সব রিসোর্স (থ্রেড, রিসিভার, ডাটাবেস কানেকশন) রিলিজ করুন
  • অ্যান্ড্রয়েড ৮+ এ Foreground Service ব্যবহার করুন দীর্ঘস্থায়ী কাজের জন্য
  • WorkManager ব্যবহার করুন ডিফারেবল ব্যাকগ্রাউন্ড ওয়ার্কের জন্য

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

  1. অ্যান্ড্রয়েড অ্যাপে WebView তৈরির পদ্ধতি

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

  3. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  4. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি