Android Background Music Service: সম্পূর্ণ গাইড
Android-এ 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>
TextView-এ ক্লিক করলে সার্ভিস স্টার্ট/স্টপ হবে।
ধাপ ৩: MainActivity.java
package com.example.myapplication;
import android.app.ActivityManager;
import android.content.Context;
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 (isMyServiceRunning(MusicService.class)) {
text.setText("Stopped");
stopService(new Intent(MainActivity.this, MusicService.class));
} else {
text.setText("Started");
startService(new Intent(MainActivity.this, MusicService.class));
}
}
});
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
ধাপ ৪: MusicService.java ক্লাস তৈরি করুন
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MusicService extends Service {
private MediaPlayer musicPlayer;
@Override
public IBinder onBind(Intent intent) {
return null; // বাউন্ড সার্ভিস না
}
@Override
public void onCreate() {
super.onCreate();
// res/raw/abc.mp3 ফাইল থাকতে হবে
musicPlayer = MediaPlayer.create(this, R.raw.abc);
musicPlayer.setLooping(false); // একবার প্লে হবে
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Music Service started", Toast.LENGTH_LONG).show();
musicPlayer.start();
return START_STICKY; // সিস্টেম কিল করলে রিস্টার্ট করবে
}
@Override
public void onDestroy() {
super.onDestroy();
if (musicPlayer != null) {
musicPlayer.stop();
musicPlayer.release();
}
Toast.makeText(this, "Music Service destroyed", Toast.LENGTH_LONG).show();
}
}
ধাপ ৫: AndroidManifest.xml-এ সার্ভিস ডিক্লেয়ার করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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=".MusicService" />
</application>
</manifest>
প্রয়োজনীয় রিসোর্স
res/raw/abc.mp3ফোল্ডার তৈরি করে একটি অডিও ফাইল রাখুন- ফাইল নাম অবশ্যই ছোট হাতের অক্ষর এবং আন্ডারস্কোর ব্যবহার করুন
লাইফসাইকেল এবং START_STICKY
- onCreate(): সার্ভিস তৈরি হলে একবার কল হয়
- onStartCommand(): প্রতিবার startService() কল করলে চলাচল করে
- START_STICKY: মেমরি কম পড়লে সিস্টেম সার্ভিস কিল করলে স্বয়ংক্রিয়ভাবে রিস্টার্ট করবে (intent null হবে)
- onDestroy(): সার্ভিস বন্ধ হলে রিসোর্স রিলিজ করুন

আধুনিক বিকল্প: MediaSession এবং Foreground Service
অ্যান্ড্রয়েড ৮.০ (API 26) থেকে ব্যাকগ্রাউন্ড এক্সিকিউশন লিমিটেশন আসায় মিডিয়া প্লেব্যাকের জন্য Foreground Service এবং MediaSessionCompat ব্যবহার করা উচিত। এটি নোটিফিকেশন শো করে এবং ইউজার কন্ট্রোল দেয়।