在 Android 12 中,Toast 会被自动截断,并且只显示一行文本。因此,我们可以使用 Snackbar、Bubble、Notification 或自定义 View 来替代 Toast。
以下是使用 Snackbar 的示例代码:
Snackbar.make(view, "This is a snackbar message", Snackbar.LENGTH_LONG).show();
以下是使用 Bubble 的示例代码:
Intent intent = new Intent(MainActivity.this, BubbleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.BubbleMetadata bubbleMetadata = new Notification.BubbleMetadata.Builder()
.setIntent(pendingIntent)
.setIcon(Icon.createWithResource(MainActivity.this, R.drawable.ic_launcher_background))
.setDesiredHeight(600)
.build();
Notification.Builder builder = new Notification.Builder(MainActivity.this, "channelId")
.setContentTitle("Bubble Title")
.setContentText("This is a bubble message")
.setSmallIcon(R.drawable.ic_launcher_background)
.setBubbleMetadata(bubbleMetadata);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
我们还可以使用 Notification 或自定义 View 来替代 Toast。通过这些方法,我们可以发送更复杂的消息,包括按钮、图像和其他高级功能。