-
Called from HomeActivity
This is the part where the service function is called from the main activity. It is called differently depending on the version.
<code>Intent serviceIntent = new Intent(HomeActivity.this, MyService.class);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(serviceIntent);} else {startService(serviceIntent);}</code><code>Intent serviceIntent = new Intent(HomeActivity.this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(serviceIntent); } else { startService(serviceIntent); } </code>Intent serviceIntent = new Intent(HomeActivity.this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(serviceIntent); } else { startService(serviceIntent); }
-
MyService Class
The
NotificationManager.IMPORTANCE_HIGH
is specified. ThesetLockscreenVisibility(Notification.VISIBILITY_PUBLIC)
is set.<code>public class MyService extends Service {private static final String CHANNEL_ID = "TTS_NOTI";private static final int NOTIFICATION_ID = 999;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {createNotificationChannel();showTTSNotification(); // Create notificationreturn START_STICKY;}private void createNotificationChannel() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"TTS Notifications",NotificationManager.IMPORTANCE_HIGH);channel.setDescription("Notifications for TTS control");channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);NotificationManager manager = getSystemService(NotificationManager.class);manager.createNotificationChannel(channel);}}private void showTTSNotification() {// MediaSession setupMediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession");mediaSession.setActive(true);// Set large iconBitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);// Create intent to bring the app to the foregroundIntent notificationIntent = new Intent(this, HomeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);// Create NotificationCompat.BuilderNotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID).setSmallIcon(R.drawable.icon).setLargeIcon(largeIcon).setContentTitle("Song Title").setContentText("Artist Name").setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_MAX).setOngoing(true).setCategory(NotificationCompat.CATEGORY_SERVICE).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken()).setShowActionsInCompactView(0, 1, 2)).addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS"))).addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE"))).addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT")));// Run the service in the foregroundstartForeground(NOTIFICATION_ID, builder.build());}private PendingIntent getPendingIntent(String action) {Intent intent = new Intent(this, NotificationReceiver.class);intent.setAction(action);return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);}@Overridepublic void onDestroy() {super.onDestroy();Log.e("MyService", "Service destroyed");}}</code><code>public class MyService extends Service { private static final String CHANNEL_ID = "TTS_NOTI"; private static final int NOTIFICATION_ID = 999; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); showTTSNotification(); // Create notification return START_STICKY; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "TTS Notifications", NotificationManager.IMPORTANCE_HIGH ); channel.setDescription("Notifications for TTS control"); channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } private void showTTSNotification() { // MediaSession setup MediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession"); mediaSession.setActive(true); // Set large icon Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon); // Create intent to bring the app to the foreground Intent notificationIntent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); // Create NotificationCompat.Builder NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setLargeIcon(largeIcon) .setContentTitle("Song Title") .setContentText("Artist Name") .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_MAX) .setOngoing(true) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken()) .setShowActionsInCompactView(0, 1, 2)) .addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS"))) .addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE"))) .addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT"))); // Run the service in the foreground startForeground(NOTIFICATION_ID, builder.build()); } private PendingIntent getPendingIntent(String action) { Intent intent = new Intent(this, NotificationReceiver.class); intent.setAction(action); return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } @Override public void onDestroy() { super.onDestroy(); Log.e("MyService", "Service destroyed"); } } </code>public class MyService extends Service { private static final String CHANNEL_ID = "TTS_NOTI"; private static final int NOTIFICATION_ID = 999; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); showTTSNotification(); // Create notification return START_STICKY; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "TTS Notifications", NotificationManager.IMPORTANCE_HIGH ); channel.setDescription("Notifications for TTS control"); channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } private void showTTSNotification() { // MediaSession setup MediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession"); mediaSession.setActive(true); // Set large icon Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon); // Create intent to bring the app to the foreground Intent notificationIntent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); // Create NotificationCompat.Builder NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setLargeIcon(largeIcon) .setContentTitle("Song Title") .setContentText("Artist Name") .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_MAX) .setOngoing(true) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken()) .setShowActionsInCompactView(0, 1, 2)) .addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS"))) .addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE"))) .addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT"))); // Run the service in the foreground startForeground(NOTIFICATION_ID, builder.build()); } private PendingIntent getPendingIntent(String action) { Intent intent = new Intent(this, NotificationReceiver.class); intent.setAction(action); return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } @Override public void onDestroy() { super.onDestroy(); Log.e("MyService", "Service destroyed"); } }
-
Parts written in AndroiManifest.xml
And I have added the necessary permissions in the
AndroidManifest
.<code><uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /><serviceandroid:name=".etc.MyService"android:foregroundServiceType="mediaPlayback"android:permission="android.permission.FOREGROUND_SERVICE"/><receiver android:name=".etc.NotificationReceiver"/></code><code><uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /> <serviceandroid:name=".etc.MyService" android:foregroundServiceType="mediaPlayback" android:permission="android.permission.FOREGROUND_SERVICE"/> <receiver android:name=".etc.NotificationReceiver"/> </code><uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /> <serviceandroid:name=".etc.MyService" android:foregroundServiceType="mediaPlayback" android:permission="android.permission.FOREGROUND_SERVICE"/> <receiver android:name=".etc.NotificationReceiver"/>
I have searched extensively on Google but have not found a solution. I would greatly appreciate any links or information that you could provide.
2