Java startForeground的错误通知(调用记录器API 29)
我在做一个电话录音程序但是, 操作\新\传出\调用权限从api 29中删除,它给出了这样一个错误。 我的目标,删除此权限使用的代码,我该怎么办?我不希望收到我们的输出信息。请帮忙 我的代码:Java startForeground的错误通知(调用记录器API 29),java,android,Java,Android,我在做一个电话录音程序但是, 操作\新\传出\调用权限从api 29中删除,它给出了这样一个错误。 我的目标,删除此权限使用的代码,我该怎么办?我不希望收到我们的输出信息。请帮忙 我的代码: public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (!action.equals(TelephonyManager.ACTION_PHO
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)
我有这样一个代码//startForeground(1,newnotification());您是否尝试使用channel id NotificationCompat.Builder(上下文,channel_id)通知我是一名新开发人员:)。我不知道怎么做。但我将学习thanksnotification=newNotificationCompat.Builder(getActivity()).setContentTitle(getActivity().getString(R.string.app_name)).setTicker(“fgfgf”).setContentText(getActivity().getString(R.string.record_enabled)).SetMallIcon(R.drawable.icon_notification).setLargeIcon(notificationLargeIconBitmap).setContentIntent(PendingContent).setContinuous(true).build();阅读此博客