<!-- Allow the application to use a foreground service --><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><!-- If the application needs to use the camera in a foreground service --><uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" /><!-- If the application needs to use the microphone in a foreground service --><uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" /><!-- Allow a foreground service to send notifications --><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
FOREGROUND_SERVICE_CAMERA and FOREGROUND_SERVICE_MICROPHONE permission declarations are required.POST_NOTIFICATIONS permission declaration is required.public class MyForegroundService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();// Create a notification channel.Notification notification = createNotification();// Handle the service startup logic.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {startForeground(1024, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);} else {startForeground(1024, notification);}}private Notification createNotification() {String CHANNEL_ONE_ID = "CHANNEL_ONE_ID";String CHANNEL_ONE_NAME = "CHANNEL_ONE_ID";NotificationChannel notificationChannel;// Check whether the Android version is 8.0 or later.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);notificationChannel.setShowBadge(true);notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);if (manager != null) {manager.createNotificationChannel(notificationChannel);}}// Set the notification click action to return to the application (optional).Intent intent = new Intent(this, MainActivity.class);ActivityOptions options = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {options = ActivityOptions.makeBasic();}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {options.setPendingIntentBackgroundActivityStartMode(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED);}PendingIntent pendingIntent;if (options != null) {pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE, options.toBundle());} else {pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {Notification notification = new Notification.Builder(this, CHANNEL_ONE_ID).setChannelId(CHANNEL_ONE_ID).setSmallIcon(R.mipmap.videocall_float_logo).setContentTitle("This is a test title").setContentIntent(pendingIntent).setContentText("This is a test content").build();notification.flags |= Notification.FLAG_NO_CLEAR;return notification;}else {NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ONE_ID).setSmallIcon(R.mipmap.videocall_float_logo).setContentTitle("This is a test title").setContentText("This is a test content").setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_DEFAULT);return builder.build();}}@Overridepublic void onDestroy() {super.onDestroy();// Stop the foreground service.stopForeground(true);}}
<serviceandroid:name=".MyForegroundService"android:enabled="true"android:exported="false"android:foregroundServiceType="mediaPlayback|mediaProjection|microphone|camera" />
android:foregroundServiceType attribute to ensure the normal operation of the background service.mediaPlayback service is used for media playback.mediaProjection service is used for media projection.microphone service is used for microphone access.camera service is used for camera access.NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);boolean areNotificationsEnabled = notificationManager.areNotificationsEnabled();if (!areNotificationsEnabled) {// Prompt the user to enable notification permissions.Toast.makeText(this, "Please enable notification permissions to ensure the normal operation of the service", Toast.LENGTH_LONG).show();// Guide the user to the settings page.Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());startActivity(intent);} else {// Start the foreground service.Intent serviceIntent = new Intent(this, MyForegroundService.class);ContextCompat.startForegroundService(this, serviceIntent);}
// Create the service Intent.Intent serviceIntent = new Intent(this, MyForegroundService.class);// Stop the service.stopService(serviceIntent);
android:stopWithTask="true" attribute value, and the service will immediately stop when the task is removed.<serviceandroid:name=".MyForegroundService"android:enabled="true"android:exported="false"android:stopWithTask="true"android:foregroundServiceType="mediaPlayback|microphone" />
@Overridepublic void onTaskRemoved(Intent rootIntent) {super.onTaskRemoved(rootIntent);// For example, execute room exit for Real-Time Communication Engine (RTC Engine) here to terminate all ongoing audio capture and playback.TRTCCloud mTRTCCloud = TRTCCloud.sharedInstance(this);mTRTCCloud.exitRoom();}
android:stopWithTask="true" is set, the onTaskRemoved method will not be called back.
// Use the call volume throughout the session.[self.trtcCloud setSystemVolumeType:TRTCSystemVolumeTypeVOIP];// Use the media volume throughout the session.[self.trtcCloud setSystemVolumeType:TRTCSystemVolumeTypeMedia];
// Enable custom audio track after room entry.[self.trtcCloud enableMixExternalAudioFrame:NO playout:YES];// Disable custom audio track before room exit.[self.trtcCloud enableMixExternalAudioFrame:NO playout:NO];
Feedback