根据https://developer.android.com/about/versions/11/behavior-changes-11#toasts,Toasts 应该仍然可以正常用于 Android11(仅 setView() 已弃用)。
请注意,仍然允许文本吐司;这些是使用不调用 setView() 的 Toast.makeText() 创建的吐司。
但是,我无法为运行 SDK30 的模拟器显示 Toast
compileSdkVersion 30
buildToolsVersion "30.0.3"
and with targetSdkVersion 30
Run Code Online (Sandbox Code Playgroud)
使用的代码只是一个简单的
Toast.makeText(getApplicationContext(), "HELLO WORLD", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
当我将 targetSdk 更改为 29(或设备 <SDK30)时,会显示吐司。
我是否需要在清单文件中为 Toast 添加任何 <queries> 标签? https://developer.android.com/about/versions/11/privacy/package-visibility
我也试过以下没有运气。
我试图使用以下代码在我的应用程序中显示一个Toast消息.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Do you want to continue?");
alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
//This code generates an Activity Not Found exception
}
catch(ActivityNotFoundException e) {
System.out.println("Activity Not Found Exception Raised");
Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
Run Code Online (Sandbox Code Playgroud)
但是这条消息仅在少数设备上显示.我已经在HTC One X上测试了这个代码,Android版本4.2.2正在运行.
如果我在Micromax A63上测试同样的代码,它也有Android 4.2.2,但它不适用于它.
我在互联网上搜索了这种错误,他们主要是在设置菜单中告诉应用程序通知禁用选项.但我的应用程序通知未被禁用.
编辑 …