Показывать маленький значок в статусе вместо одной белой точки службой переднего плана.Android

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Показывать маленький значок в статусе вместо одной белой точки службой переднего плана.

Сообщение Anonymous »

Я пишу код службы переднего плана и устанавливаю «setSmallIcon», когда он запускается, значок не отображается в строке состояния. мне нужны первое и второе изображения, как показано ниже:
Изображение

Изображение

и ниже это не то, что я хочу:
Изображение

Я написал код службы переднего плана ниже:

Код: Выделить всё

public class MyForegroundService extends Service {

private static final String CHANNEL_ID = "MyForegroundServiceChannel";

@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
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)
.setContentTitle("My Foreground Service")
.setContentText("Running...")
.setSmallIcon(R.drawable.ic_launcher_foreground) // 使用默认图标
.setContentIntent(pendingIntent)
.build();

startForeground(1, notification);
return START_NOT_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
ниже находится MainActivity.java:

Код: Выделить всё

Intent serviceIntent = new Intent(this, MyForegroundService.class);
startForegroundService(serviceIntent);
и я написал код разрешения в манифесте.xml, включая: post_notification и передний план, возраст несколько дней, мне удалось, но вчера я удалил код по ошибке и не смог найти код, который я написал .

Подробнее здесь: https://stackoverflow.com/questions/793 ... nd-service
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»