[*] вызывается из Homeactivity < /p>
Это часть, в которой функция обслуживания вызывается из основной деятельности. Это называется по-разному в зависимости от версии.
Код: Выделить всё
Intent serviceIntent = new Intent(HomeActivity.this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
} else {
startService(serviceIntent);
}
Уведомлено уведомление manager.importance_high . Setlock-Screenvisibility (natification.visibility_public) установлен.
Код: Выделить всё
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");
}
}
, и я добавил необходимые разрешения в Androidmanifest .
< /code>
< /li>
< /ol>
Я широко искал в Google, но не нашел решение. Я бы очень признателен за любые ссылки или информацию, которые вы могли бы предоставить.
Подробнее здесь: https://stackoverflow.com/questions/789 ... on-lock-sc