Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android startForeground失败,startForeground的通知错误_Android_Kotlin_Service - Fatal编程技术网

Android startForeground失败,startForeground的通知错误

Android startForeground失败,startForeground的通知错误,android,kotlin,service,Android,Kotlin,Service,我正在尝试启动前台服务,但失败了 val notificationIntent=Intent(这是MainActivity::class.java) val pendingent=pendingent.getActivity(this,0,notificationIntent,0) val builder:NotificationCompat.builder 建筑商= 如果(Build.VERSION.SDK_INT>=26){ val channelId=“非信道” val通道=Notific

我正在尝试启动前台服务,但失败了

val notificationIntent=Intent(这是MainActivity::class.java)
val pendingent=pendingent.getActivity(this,0,notificationIntent,0)
val builder:NotificationCompat.builder
建筑商=
如果(Build.VERSION.SDK_INT>=26){
val channelId=“非信道”
val通道=NotificationChannel(通道ID,“通知通道”,NotificationManager.IMPORTANCE\u默认值)
if(!GetSharedReferences(“Imhere”,Context.MODE_PRIVATE).getBoolean(“isNotificationCreated”,false)){
(getSystemService(Context.NOTIFICATION_服务)作为NotificationManager)。createNotificationChannel(通道)
GetSharedReferences(“Imhere”,Context.MODE_PRIVATE).edit().putBoolean(“isNotificationCreated”,true).apply()
}
NotificationCompat.Builder(此,channelId)
}否则{
NotificationCompat.Builder(此)
}
建设者
.setContentTitle(“位置服务”)
.setContentText(“在前台运行的服务”)
.setContentIntent(挂起内容)
startForeground(502,builder.build())
此代码在服务本身中,所以ServiceOnCreate方法调用此代码块 上次它工作正常,但当我删除应用程序并重新安装时,它开始失败

2020-08-30 05:08:19.233 706-706/{app package name} E/AndroidRuntime: FATAL EXCEPTION: main
        Process: {app package name}, PID: 706
        android.app.RemoteServiceException: Bad notification for startForeground
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2141)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:237)
            at android.app.ActivityThread.main(ActivityThread.java:8016)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
我的代码怎么了?

自我回答: 这是因为备份内容。 在我的代码中,我使用SharedReference检查通知通道是否已创建。但是
BackupManager
即使在删除应用程序后也保存了我的SharedReference,因此在重新安装应用程序时,不会创建通知频道。 我更改了
AndroidManifest.xml
,如下所示,它清除了我的SharedReference。现在它工作得很好


您尝试过在通知中添加图标吗?@commonware是的,我尝试过使用
.setSmallIcon(R.drawable)
,但它也不起作用。