Android Toast消息受到Rom的影响

27 阅读1分钟

服了,Toast都被Rom影响了

由于开发过程中,使用Toast时,总是看到消息的前面加上了app_name:消息,刚开始还以为是程序本来如此,后来才发现,有可能是手机Rom的问题。

通过以下的操作,就能覆盖Rom定制过的影响。

val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
            toast.setText(text)
            toast.show()
fun showText(context: Context, text: String) {
        if (text.isEmpty()) {
            return
        }

        if (oldMsg.isEmpty()) {
            val toast = Toast.makeText(context, text, Toast.LENGTH_SHORT)
            toast.setText(text)
            toast.show()
            oldMsg = text
            oneTime = System.currentTimeMillis()
        } else {
            twoTime = System.currentTimeMillis()
            if (twoTime - oneTime > 2000) {
                val toast = Toast.makeText(
                    context,
                    text,
                    Toast.LENGTH_SHORT
                )
                toast.setText(text)
                toast.show()
                oneTime = twoTime
                oldMsg = text
            } else {
                if (oldMsg != text) {
                    val toast = Toast.makeText(
                        context,
                        text,
                        Toast.LENGTH_SHORT
                    )
                    toast.setText(text)
                    toast.show()
                    oneTime = twoTime
                }
            }
        }
    }

还有Rom定制带上appIcon的情况

国产手机有些部分会定制Rom中修改Toast的样式,只要setText覆盖就行