产品概述
产品优势
应用场景
<!-- 允许应用使用前台服务 --><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><!-- 如果应用需要在前台服务中使用摄像头 --><uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" /><!-- 如果应用需要在前台服务中使用麦克风 --><uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" /><!-- 允许前台服务发送通知 --><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
FOREGROUND_SERVICE_CAMERA 和 FOREGROUND_SERVICE_MICROPHONE 权限声明是必须的。POST_NOTIFICATIONS 权限。public class MyForegroundService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();// 创建通知渠道Notification notification = createNotification();// 处理服务启动逻辑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;//进行8.0的判断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);}}// 设置点击通知栏回到应用,可选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("这是一个测试标题").setContentIntent(pendingIntent).setContentText("这是一个测试内容").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("这是一个测试标题").setContentText("这是一个测试内容").setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_DEFAULT);return builder.build();}}@Overridepublic void onDestroy() {super.onDestroy();// 停止前台服务stopForeground(true);}}
<serviceandroid:name=".MyForegroundService"android:enabled="true"android:exported="false"android:foregroundServiceType="mediaPlayback|mediaProjection|microphone|camera" />
android:foregroundServiceType 属性指定前台服务需要使用的服务类型,以确保后台可以保持正常的服务功能。mediaPlayback 服务用于媒体播放。mediaProjection 服务用于媒体投影。microphone 服务用于使用麦克风。camera 服务用于使用摄像头。NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);boolean areNotificationsEnabled = notificationManager.areNotificationsEnabled();if (!areNotificationsEnabled) {// 提示用户启用通知权限Toast.makeText(this, "请启用通知权限以确保服务正常运行", Toast.LENGTH_LONG).show();// 引导用户到设置页面Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());startActivity(intent);} else {// 启动前台服务Intent serviceIntent = new Intent(this, MyForegroundService.class);ContextCompat.startForegroundService(this, serviceIntent);}
// 创建服务的IntentIntent serviceIntent = new Intent(this, MyForegroundService.class);// 停止服务stopService(serviceIntent);
android:stopWithTask="true" 属性值,服务会在任务被移除时立即停止。<serviceandroid:name=".MyForegroundService"android:enabled="true"android:exported="false"android:stopWithTask="true"android:foregroundServiceType="mediaPlayback|microphone" />
@Overridepublic void onTaskRemoved(Intent rootIntent) {super.onTaskRemoved(rootIntent);// 例如在这里执行 RTC Engine 退房,避免继续进行音频采集和播放TRTCCloud mTRTCCloud = TRTCCloud.sharedInstance(this);mTRTCCloud.exitRoom();}
android:stopWithTask="true" 之后,onTaskRemoved 方法将不会被回调。
// 指定全程通话音量[self.trtcCloud setSystemVolumeType:TRTCSystemVolumeTypeVOIP];// 指定全程媒体音量[self.trtcCloud setSystemVolumeType:TRTCSystemVolumeTypeMedia];
// 进房之后启用自定义音轨[self.trtcCloud enableMixExternalAudioFrame:NO playout:YES];// 退房之前关闭自定义音轨[self.trtcCloud enableMixExternalAudioFrame:NO playout:NO];
文档反馈