Android13应用在后台录音无声音

最近在做项目,对讲应用放在后台,录音无声音,最后解决。

一 现象

对讲应用运行在后台,录音无效查看日志,AudioRecorder录音回调全是0;状态栏无通知,无申请通知权限。

二解决

看了现象应该能够猜测到解决方案了。让应用在后台运行不被挂起,需要申请通知权限,然后创建通知在状态栏显示,防止应用被挂起。

1 申请通知权限

AndroidManifest.xml中

html 复制代码
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
2 代码检查权限
java 复制代码
private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(
            new ActivityResultContracts.RequestPermission(),
            result -> {
                if (result == null || !result) {
                    // 用户不允许权限,处理拒绝的情况
                    ToastUtil.getInstance().show("请授予通知权限");
                } else {
                    // 用户允许权限,处理接受的情况

                }
            });

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public void requestPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            // 权限未被授予,向用户请求权限
            requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
        } else {
            // 权限已经被授予,可以执行操作
        }
    }
3 创建通知栏 ,在service中创建
java 复制代码
@SuppressLint("NotificationPermission")
    private void showMessageNotification(String data) {
        Log.e(TAG, "showMessageNotification data: " + data);

        String type = data.substring(0, 2);
        String a = ConvertUtil.convertBigModel(data.substring(2, 10));
        //短信发送方号码
        String number = String.valueOf(Long.parseLong(a, 16));
        //联系人号码
//        String b = ConvertUtil.convertBigModel(data.substring(10, 18));
//        String number1 = String.valueOf(Long.parseLong(b, 16));
        String text = ConvertUtil.unicodeToString(data.substring(10));

        Log.e(TAG, "showMessageNotification: " + data);
        // 为该通知设置一个id
        int notifyID = 1;

        daoManager = DaoManager.getInstance(this);
        smsLists = daoManager.querySmsList();
        if (smsLists != null && smsLists.size() > 0) {
            SmsList sms = daoManager.querySms(smsLists.get(smsLists.size() - 1).getId());
            notifyID = (int) (sms.getId() + 1);
        }

        int current = SPUtil.getInt(this, Constant.CHANNEL_INDEX, Constant.CHANNEL_INDEX_DEFAULT);
        list = new SmsList(notifyID, System.currentTimeMillis(), current, type, number, text, false);
        daoManager.insertSmsList(list);

        //创建通知
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // 通知渠道的id
            String id = "my_channel_dmr_message";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel mChannel = new NotificationChannel(id, number, importance);
            mChannel.setShowBadge(true);
            mChannel.setName(getString(R.string.message));
            mNotificationManager.createNotificationChannel(mChannel);

            //设置点击事件
            Intent clickIntent = new Intent(this, SmsActivity.class);
            clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent clearIntent = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_IMMUTABLE);

            // 通知渠道的id
            String CHANNEL_ID = "my_channel_dmr_message";
            // Create a notification and set the notification channel.
            Notification notification = new Notification.Builder(this, CHANNEL_ID)
                    .setAutoCancel(true)
                    .setShowWhen(true)
                    .setContentIntent(clearIntent)
                    .setContentTitle(number)
                    .setContentText(text)
                    .setSmallIcon(R.drawable.icon_message)
                    .setChannelId(CHANNEL_ID)
                    .build();
            mNotificationManager.notify(notifyID, notification);
        }

       
    }
4 在AndroidManifest.xml中给service增加audioRecorder使用权限
html 复制代码
 <service
            android:priority="1000"
          +  android:foregroundServiceType="microphone"
            android:name=".server.BoService" />

END~

相关推荐
千里马学框架2 小时前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
xianrenli387 小时前
android特许权限调试
android
*拯10 小时前
Uniapp Android/IOS 获取手机通讯录
android·ios·uni-app
天天打码12 小时前
Lynx-字节跳动跨平台框架多端兼容Android, iOS, Web 原生渲染
android·前端·javascript·ios
lilili啊啊啊14 小时前
iOS safari和android chrome开启网页调试与检查器的方法
android·ios·safari
Blue.ztl16 小时前
菜鸟之路day31一一MySQL之多表设计
android·数据库·mysql
练习本20 小时前
Android系统架构模式分析
android·java·架构·系统架构
每次的天空1 天前
Kotlin 内联函数深度解析:从源码到实践优化
android·开发语言·kotlin
练习本1 天前
Android MVC架构的现代化改造:构建清晰单向数据流
android·架构·mvc
早上好啊! 树哥1 天前
android studio开发:设置屏幕朝向为竖屏,强制应用的包体始终以竖屏(纵向)展示
android·ide·android studio