package com.ss.demo.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import com.ss.video.rtc.demo.ui.chat.ChatActivity;
public class RoomKeepLifeService extends Service {
public static final String CHANNEL_ID = "RoomKeepLifeServiceChannel";
private static final String COMMAND = "command";
private static final String COMMAND_START = "start";
private static final String COMMAND_STOP = "stop";
public IBinder onBind(Intent intent) {
public void onCreate() {
startAsForeground(new Intent());
public int onStartCommand(Intent intent, int flags, int startId) {
switch (intent.getStringExtra(COMMAND)){
startAsForeground(intent);
return super.onStartCommand(intent, flags, startId);
public void stopInternal() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
private void startAsForeground(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
createNotificationChannel();
Intent notificationIntent = new Intent(this, ChatActivity.class); // 请替换为实际的 Activity
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("房间正在进行中...")
.setSmallIcon(com.ss.video.rtc.common.R.drawable.icon_default_avatar)
.setContentIntent(pendingIntent)
startForeground(110, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
NotificationChannel serviceChannel = new NotificationChannel(
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);