簡體   English   中英

startForeground 的錯誤通知(Call Recorder API 29)

[英]Bad notification for startForeground (Call Recorder API 29)

我正在做一個通話記錄程序。但是,從 api 29 中刪除了 ACTION_NEW_OUTGOING_CALL 權限,它給出了這樣的錯誤。 我的目標是刪除此權限使用的代碼,我該怎么辦?我不想傳入我們的輸出信息。 請幫忙!

我的代碼:

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 創建通知通道

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

 NotificationCompat.Builder(context, CHANNEL_ID)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM