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

অ্যান্ড্রয়েডে এক্টিভিটি এবং সার্ভিসের মধ্যে যোগাযোগের পদ্ধতি

Start Service এবং Intent ব্যবহার করে এক্টিভিটি-সার্ভিস যোগাযোগ

অ্যান্ড্রয়েডে এক্টিভিটি এবং সার্ভিসের মধ্যে যোগাযোগের সবচেয়ে সহজ পদ্ধতি হলো `startService()` এবং `stopService()` ব্যবহার করে কমান্ড পাঠানো। এই উদাহরণে একটি মিউজিক প্লেয়ার সার্ভিস তৈরি করা হয়েছে যা ব্যাকগ্রাউন্ডে গান বাজায়।

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

স্ট্যান্ডার্ড অ্যান্ড্রয়েড প্রজেক্ট।

ধাপ ২: লেআউট - স্টার্ট/স্টপ বাটন

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">
    <Button
        android:id="@+id/buttonStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:text="Start Service" />
    <Button
        android:id="@+id/buttonStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Stop Service" />
</RelativeLayout>

ধাপ ৩: মেইন এক্টিভিটি

src/MainActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button buttonStart, buttonStop;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonStart = findViewById(R.id.buttonStart);
        buttonStop = findViewById(R.id.buttonStop);
        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View src) {
        switch (src.getId()) {
            case R.id.buttonStart:
                startService(new Intent(this, MyService.class));
                break;
            case R.id.buttonStop:
                stopService(new Intent(this, MyService.class));
                break;
        }
    }
}

ধাপ ৪: সার্ভিস ক্লাস তৈরি করুন

src/MyService.java - নতুন জাভা ক্লাস (Service এক্সটেন্ড করে):

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    MediaPlayer mediaPlayer;
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null; // Started service, binding নেই
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        mediaPlayer = MediaPlayer.create(this, R.raw.song); // res/raw/song.mp3 দরকার
        mediaPlayer.setLooping(false);
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        mediaPlayer.start();
        return START_STICKY; // সিস্টেম কিল করলে রিস্টার্ট হবে
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}

ধাপ ৫: রস ফোল্ডারে অডিও ফাইল

  1. `res/raw/` ফোল্ডার তৈরি করুন (New → Android Resource Directory → raw)
  2. এতে একটি অডিও ফাইল রাখুন (যেমন `song.mp3`)

ধাপ ৬: ম্যানিফেস্টে সার্ভিস ডিক্লেয়ার করুন

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <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">
        
        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
        </service>
        
        <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>

আধুনিক বিকল্প: Foreground Service (Android 8.0+)

Android 8.0 (API 26) থেকে ব্যাকগ্রাউন্ড সার্ভিস সীমাবদ্ধ। মিডিয়া প্লেব্যাকের জন্য Foreground Service ব্যবহার করুন:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // নোটিফিকেশন তৈরি করুন
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle("Playing Music")
        .setContentText("Song name")
        .setSmallIcon(R.drawable.ic_music)
        .build();
    
    startForeground(1, notification); // Foreground service
    mediaPlayer.start();
    return START_STICKY;
}

আরও যোগাযোগের পদ্ধতি

  • Bound Service: `bindService()` - IPC, কলব্যাক, রিয়েল-টাইম ডেটা এক্সচেঞ্জ
  • BroadcastReceiver: সার্ভিস থেকে ব্রডকাস্ট পাঠিয়ে এক্টিভিটি আপডেট
  • EventBus/Otto: থার্ড-পার্টি ইভেন্ট বাস লাইব্রেরি
  • LiveData/Flow: আধুনিক আর্কিটেকচার কম্পোনেন্টস
অ্যান্ড্রয়েডে এক্টিভিটি এবং সার্ভিসের মধ্যে যোগাযোগের পদ্ধতি অ্যান্ড্রয়েডে এক্টিভিটি এবং সার্ভিসের মধ্যে যোগাযোগের পদ্ধতি
  1. অ্যান্ড্রয়েডে fill_parent এবং wrap_content এর পার্থক্য

  2. অ্যান্ড্রয়েডে এক্টিভিটির মাঝপথে Drawable পাস করা

  3. অ্যান্ড্রয়েডে AlarmManager ব্যবহার করে সার্ভিস শুরু করার পদ্ধতি

  4. অ্যান্ড্রয়েডে এক্টিভিটির থেকে ফ্র্যাগমেন্টে ভেরিয়েবল পাস করার পদ্ধতি