Notifying the user

本文介绍了三种应用程序通知技术:ToastNotification用于短暂消息;StatusBarNotification用于后台进程提醒用户并请求响应;DialogNotification用于活动相关通知,如进度条或需要用户确认的消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Several types of situations may arise that require you to notify the user about an event that occurs in your application. Some events require the user to respond and others do not. For example:

  • When an event such as saving a file is complete, a small message should appear to confirm that the save was successful.
  • If the application is running in the background and needs the user's attention, the application should create a notificaiton that allows the user to respond at his or her convenience.
  • If the application is performing work that the user must wait for (such as loading a file), the application should show a hovering progress wheel or bar.

Each of these notification tasks can be achieved using a different technique:

This document summarizes each of these techniques for notifying the user and includes links to full documentation.

Toast Notification

A toast notificaiton is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events. Because a toast can be created from a background Service, it appears even if the application isn't visible.

A toast is best for short text messages, such as "File saved," when you're fairly certain the user is paying attention to the screen. A toast can not accept user interaction events; if you'd like the user to respond and take action, consider using a Status Bar Notification instead.

For more information, refer to Creating Toast Notifications.

Status Bar Notification

A status bar notification adds an icon to the system's status bar (with an optional ticker-text message) and an expanded message in the "Notifications" window. When the user selects the expanded message, Android fires an Intent that is defined by the notification (usually to launch an Activity). You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device.

This kind of notification is ideal when your application is working in a background Service and needs to notify the user about an event. If you need to alert the user about an event that occurs while your Activity is still in focus, consider using a Dialog Notification instead.

For more information, refer to Creating Status Bar Notifications.

Dialog Notification

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications and short activities that directly relate to the application in progress.

You should use a dialog when you need to show a progress bar or a short message that requires confirmation from the user (such as an alert with "OK" and "Cancel" buttons). You can use also use dialogs as integral componenents in your application's UI and for other purposes besides notifications. For a complete discussion on all the available types of dialogs, including its uses for notifications, refer to Creating Dialogs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值