製品概要
製品の利点
応用シナリオ
<!-- アプリケーションがフロントエンドサービスを使用することを許可 --><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);}
// サービスのIntentを作成Intent 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);// 例えば、ここでReal-Time Communication Engine (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];
フィードバック