微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在 android 系统设置API 29+上显示 Toast?

如何解决是否可以在 android 系统设置API 29+上显示 Toast?

以前我用过的吐司

Handler().postDelayed(DELAY){
   // show toast 
}

         

用户导航到 android 设置中的某个位置。从 API 29 开始,此吐司不再出现。如果我在 DELAY 结束之前按下主页按钮,它会在 android 桌面上显示没有问题。

  1. 自 API 29 以来,是否有一些未经宣布的更改,即不再可能在 android 设置上显示 Toast?

  2. 还能以某种方式在那里展示 Toast 吗?

解决方法

Toast 仍然为我工作,如果你想在线程内显示 Toast,请使用:

 new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
     public void run() {
         Toast.makeText(getApplicationContext(),"Hi !",Toast.LENGTH_SHORT).show();
       }
   });

但是,如果这对您不起作用,您可以在任何地方使用自定义 Toast 消息。首先,创建 CustomeToast 类:

   public class CustomToast {

       public void showMessage(Context context,String message){

           LayoutInflater inflater = (LayoutInflater) context.getSystemService( 
           Context.LAYOUT_INFLATER_SERVICE );
           View view =inflater.inflate(R.layout.toast,null);
           View layout = view.findViewById(R.id.toast_layout_root);

           TextView text = (TextView) layout.findViewById(R.id.text);
           text.setText(message);

           Toast toast = new Toast(context);
           toast.setGravity(Gravity.CENTER_VERTICAL,0);
           toast.setDuration(Toast.LENGTH_LONG);
           toast.setView(layout);
           toast.show();
    }
}

然后随心所欲地使用:

 CustomToast customToast= new CustomToast();       
 customToast.showMessage(getApplicationContext(),"Hi !");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。