启动前台(呼叫记录器 API 29)的错误通知



我正在做一个通话记录程序。但 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

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)

最新更新