Foreground Service এবং startForeground() বাস্তবায়ন
অ্যান্ড্রয়েড ৮.০ (API 26) থেকে ব্যাকগ্রাউন্ড সার্ভিসে সীমাবদ্ধতা আসায় দীর্ঘস্থায়ী টাস্কের জন্য Foreground Service ব্যবহার করা বাধ্যতামূলক। এটি একটি নোটিফিকেশন দেখায় যাতে ইউজার জানতে পারে সার্ভিস চলছে।
ধাপ ১-২: প্রজেক্ট ও লেআউট (পূর্বের মতো)
একটি TextView থাকবে যা ক্লিক করলে সার্ভিস টগল করবে।
ধাপ ৩: Foreground Service ক্লাস
package com.example.myapplication;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import android.widget.Toast;
public class ForegroundService extends Service {
private static final String CHANNEL_ID = "com.example.myapplication.foreground";
private static final int NOTIFICATION_ID = 1337;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Foreground Service Started", Toast.LENGTH_LONG).show();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Awesome App")
.setContentText("Service running in foreground")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
// সার্ভিসকে ফোরগ্রাউন্ডে চালু করুন
startForeground(NOTIFICATION_ID, notification);
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true); // নোটিফিকেশনও রিমুভ হবে
Toast.makeText(this, "Foreground Service Destroyed", Toast.LENGTH_LONG).show();
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"My Background Service",
NotificationManager.IMPORTANCE_LOW
);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setDescription("Foreground service notification channel");
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
}
}
গুরুত্বপূর্ণ মেথডসমূহ
- startForeground(id, notification): সার্ভিসকে ফোরগ্রাউন্ডে বপাস করে, নোটিফিকেশন বাধ্যতামূলক
- stopForeground(removeNotification): true দিলে নোটিফিকেশনও রিমুভ হয়
- NotificationChannel: API 26+ এ নোটিফিকেশন চ্যানেল বাধ্যতামূলক
- PendingIntent.FLAG_IMMUTABLE: API 23+ এ সিকিউরিটির জন্য বাধ্যতামূলক
MainActivity.java (সার্ভিস কন্ট্রোল)
public class MainActivity extends AppCompatActivity {
// ... (পূর্বের কোডটি ব্যবহার করুন, শুধু সার্ভিস ক্লাস নাম পরিবর্তন করুন)
// isMyServiceRunning(ForegroundService.class)
// startService(new Intent(this, ForegroundService.class))
}
Manifest-এ সার্ভিস ডিক্লেয়ারেশন (API 29+ এর জন্য foregroundServiceType)
<service
android:name=".ForegroundService"
android:foregroundServiceType="mediaPlayback" />
<!-- অথবা location, microphone, camera, phoneCall ইত্যাদি যেটি প্রয়োজন -->
সכেনারিও যেখানে Foreground Service বাধ্যতামূলক
- মিডিয়া প্লেব্যাক (Music, Video)
- লোকেশন ট্র্যাকিং (Navigation, Fitness)
- ফাইল ডাউনলোড/আপলোড
- রিয়েল-টাইম কমিউনিকেশন (VoIP, Chat)
