问题描述
以前我用过的吐司
Handler().postDelayed(DELAY){
// show toast
}
将用户导航到 android 设置中的某个位置。从 API 29 开始,此吐司不再出现。如果我在 DELAY 结束之前按下主页按钮,它会在 android 桌面上显示没有问题。
-
自 API 29 以来,是否有一些未经宣布的更改,即不再可能在 android 设置上显示 Toast?
-
还能以某种方式在那里展示 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 !");