首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >startForeground的错误通知(调用Recorder API 29)

startForeground的错误通知(调用Recorder API 29)
EN

Stack Overflow用户
提问于 2020-01-20 23:01:31
回答 1查看 77关注 0票数 0

我正在做一个通话记录器program.But,ACTION_NEW_OUTGOING_CALL权限从API29中删除,它给出这样的错误。我的目标,删除这个权限使用的代码,我该怎么做?我不想输入我们的输出信息。请帮帮我!

我的代码:

代码语言:javascript
运行
复制
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);
        }
    }

但是当我打开这个应用程序时,我看到了这个错误:

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-20 23:04:49

你应该为android.O创建通知通道

代码语言:javascript
运行
复制
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)
        }

在创建通知时,您应该给它提供通道id。

代码语言:javascript
运行
复制
 NotificationCompat.Builder(context, CHANNEL_ID)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59825937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档