java - startForeground 的错误通知(Call Recorder API 29)

标签 java android

我正在做一个通话录音程序。但是, ACTION_NEW_OUTGOING_CALL 权限从 api 29 中删除,它给出了这样的错误。 我的目标是删除此权限使用的代码,我该怎么办?我不想传入我们的结果信息。请帮忙!

我的代码:

public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED) &&
                !action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            return;
        }

        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        String extraState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        Log.d(Constants.TAG, "PhoneReceiver phone number: " + phoneNumber);
        if (!FileHelper.isStorageWritable(context))
            return;
        if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
            App.isOutComming = true;
        }
        if (extraState != null) {
            dispatchExtra(context, intent, phoneNumber, extraState);
        } else if (phoneNumber != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(new Intent(context, RecordService.class)
                        .putExtra("commandType", Constants.STATE_INCOMING_NUMBER)
                        .putExtra("phoneNumber", phoneNumber));
            } else {
                context.startService(new Intent(context, RecordService.class)
                        .putExtra("commandType", Constants.STATE_INCOMING_NUMBER)
                        .putExtra("phoneNumber", phoneNumber));
            }
        } else {
            Log.d(Constants.TAG, "phone number x:x " + null);
        }
    }

但是当我启动此应用程序时,我看到此错误:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)

最佳答案

您应该为 android.O 创建通知 channel

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = android.app.NotificationManager.IMPORTANCE_LOW
            val mChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance)
            mChannel.description = CHANNEL_DESCRIPTION
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            val mNotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
            mNotificationManager.createNotificationChannel(mChannel)
        }

创建通知时,您应该为其提供 channel ID

 NotificationCompat.Builder(context, CHANNEL_ID)

关于java - startForeground 的错误通知(Call Recorder API 29),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825937/

相关文章:

用于振动和静音的 Android BroadCast 接收器

Java编译器字符串优化

java - 记录集的问题

android - SQLite 数据库不创建 android

android - Seekbar中没有标记颜色

java - 如果在 ListPreference 上单击“取消”,如何不保存到 SharedPreferences

java - 在一段时间后设置可见的 Android 按钮?

java - Hibernate注解对象映射

java - 使用 java 的 Play Framework 的微服务模板项目

Android Shake(Sensor) 服务,用于在应用程序后台进行抖动检测