【问题标题】:Is it possible to show toast over android system settings (API 29+)?是否可以在 android 系统设置(API 29+)上显示吐司?
【发布时间】:2021-04-07 08:44:36
【问题描述】:

以前我有一个用过的吐司

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

         

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

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

  2. 是否还能以某种方式在那里展示吐司?

【问题讨论】:

    标签: android toast android-version


    【解决方案1】:

    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, 0);
               toast.setDuration(Toast.LENGTH_LONG);
               toast.setView(layout);
               toast.show();
        }
    }
    

    然后在任何你想要的地方使用:

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

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多