安卓端使用RTC Lib进行音视频通话,APP切入后台约1分钟后系统禁止用户访问麦克风问题

已邀请:

主要思路:使用前台服务

前台服务被认为是用户主动意识的服务且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,放在"正在运行"标题下方,这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。

创建服务,主要代码如下:

import android.app.Service;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

public class RTCNotificationService extends Service {

    private NotificationManager mNotificationManager;
    private static final String channelId = "RTCNotificationService";
    // 该值请用户根据业务自定义
    private int notifyId = 20200202;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //CallActivity.class点击通知栏后打开的Activity,该Activity的launchMode设置为:android:launchMode="singleTop"
            Intent intent=new Intent(this, CallActivity.class);
            intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            Notification.Builder builder = new Notification.Builder(this)
                    //R.drawable.ic_launcher :通知栏显示的图标
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setTicker(getString(R.string.app_name))
                    //通知栏提示语
                    .setContentTitle(getString(R.string.TapToContiueAsVideoCallIsOn))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder.setCategory(Notification.CATEGORY_CALL);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder.setChannelId(channelId);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel notificationChannel = new
                        NotificationChannel(channelId, "test", importance);
                notificationChannel.enableLights(false);
                notificationChannel.setLightColor(Color.GREEN);
                notificationChannel.enableVibration(false);
                notificationChannel.setSound(null, null);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
            mNotificationManager.notify(notifyId, builder.build());
            startForeground(notifyId, builder.build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mNotificationManager.cancel(notifyId);
        stopForeground(true);
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

在Application的onCreate()回调中监听APP前后台切换,主要代码如下:

    private int mActiveCount = 0;
    private int mAliveCount=0;
    private boolean isActive;

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

    private void registerLifecycleCallbacks() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                mAliveCount++;
            }

            @Override
            public void onActivityStarted(Activity activity) {
                mActiveCount++;
                notifyChange();
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                mActiveCount--;
                notifyChange();
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                mAliveCount--;
                if (mAliveCount == 0) {
                    stopNotificationService();
                }
            }
        });
    }

    private void notifyChange() {
        if (mActiveCount > 0) {
            if (!isActive) {
                isActive = true;
                //APP进入前台,关闭前台服务
                stopNotificationService();
            }
        } else {
            if (isActive) {
                isActive = false;
                //APP被切入后台,CenterManager.getInstance().isInRoom();是判断是否在音视频房间中;
                if (CenterManager.getInstance().isInRoom()) {
                    startService(new Intent(this, RTCNotificationService.class));
                }
            }
        }
    }
    private void stopNotificationService() {
        //CenterManager.getInstance().isInRoom();是判断是否在音视频房间中;
        if (CenterManager.getInstance().isInRoom()) {
            boolean val=stopService(new Intent(Application.this, RTCNotificationService.class));
        }
    }

manifest文件中注册Service:

要回复问题请先登录注册