You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android API 31及以上关闭通知时Toast消息不显示的解决求助

API 31升级后关闭通知时Toast无法显示的解决方案

可行方案列表

  • 改用Snackbar替代Toast
    Snackbar属于应用View层级组件,不受通知权限状态影响,兼容性更好。示例代码:

    Snackbar.make(findViewById(android.R.id.content), "你的提示内容", Snackbar.LENGTH_SHORT).show();
    

    可以自定义布局和样式,完全适配现有Toast的提示场景。

  • 降级为低优先级通知(备选方案)
    当检测到通知权限关闭时,将Toast提示转为发送一条低优先级通知,不会过多打扰用户。示例代码:

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    // 创建通知渠道(API 26+必填)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel("toast_backup", "Toast备用通知", NotificationManager.IMPORTANCE_LOW)
        notificationManager.createNotificationChannel(channel)
    }
    val notification = NotificationCompat.Builder(this, "toast_backup")
        .setSmallIcon(R.drawable.ic_default_notify)
        .setContentText("你的提示内容")
        .setPriority(NotificationCompat.PRIORITY_LOW)
        .setAutoCancel(true)
        .build()
    notificationManager.notify(1001, notification)
    
  • 修正Toast的上下文使用
    API 31对后台Toast的显示限制更严格,避免使用Application Context显示Toast,改用当前Activity或View的上下文,能提升显示成功率。示例代码:

    Toast.makeText(this@MainActivity, "你的提示内容", Toast.LENGTH_SHORT).show();
    
  • 手动基于WindowManager实现自定义Toast
    直接通过WindowManager添加自定义View模拟Toast效果,完全摆脱通知权限限制。示例代码:

    // 加载自定义Toast布局
    View customToast = LayoutInflater.from(this).inflate(R.layout.layout_custom_toast, null);
    TextView toastText = customToast.findViewById(R.id.tv_toast_content);
    toastText.setText("你的提示内容");
    
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    // 设置窗口类型,API 26+用TYPE_APPLICATION_OVERLAY
    params.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 
        ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 
        : WindowManager.LayoutParams.TYPE_TOAST;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    params.y = dpToPx(this, 60); // 底部偏移60dp
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    params.format = PixelFormat.TRANSLUCENT;
    
    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(customToast, params);
    
    // 2秒后移除View,模拟Toast时长
    new Handler(Looper.getMainLooper()).postDelayed(() -> {
        if (customToast.isAttachedToWindow()) {
            windowManager.removeView(customToast);
        }
    }, 2000);
    

    注意:Android 6.0及以上需要申请SYSTEM_ALERT_WINDOW权限,需引导用户在系统设置中手动开启。

内容的提问来源于stack exchange,提问作者YuzuKi9527

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:30:49