android – startForeground()总是在Samsung 8.0设备上显示一个弹出窗口

每次我在前台服务中使用startForeground()在其通知中进行状态更新时,我遇到三星Galaxy S7 Edge的问题,显示弹出通知.

首先,此问题出现在所有Android 8.0设备上,但很容易修复,我的通知通道优先级设置为IMPORTANCE_LOW.但问题仍然存在于三星设备上.

所以,问题是,如何在Samsung 8.0设备上静默更新前台服务通知?

我的代码如下.

申请类:

override fun onCreate() {
        super.onCreate()
        //other code
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannels(listOf(
                    //
                    NotificationChannel(MY_NOTIFICATION_CHANNEL,"My channel",NotificationManager.IMPORTANCE_LOW).apply {
                        setSound(null,null)
                    }
                    //
            ))
        }
    }

服务开始前景:

val notification = NotificationCompat.Builder(this,MY_NOTIFICATION_CHANNEL)
                .setSmallIcon(android.R.drawable.stat_sys_warning)
                .setColor(App.context().resources.getColor(R.color.primary_dark))
                .setContentText(if (/*logic*/) "This" else "That")
                .addAction(0,if (enable) "Disable" else "Enable",firstIntent)
                .addAction(0,"Another action",secondIntent)
                .build()

        startForeground(MY_NOTIFICATION_ID,notification)

解决方法

我一直在调查.您可以做的最好的事情是创建没有频道的通知.服务从托盘中的任何内容开始.似乎适合我.

NotificationCompat.Builder(本)

但是,此构造函数在26.1.0中已弃用

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...