Android UI学习 - 用户通知

简介:

本文是在网上的文章《Android开发指南-用户界面-用户通知》的基础上添加内容的。里面的相同内容,版权归原翻译作者所有。

通知用户Notifying the User

    某些情况下需要通知用户你的应用程序中发生了一个事件。一些事件请求用户应答而另外一些则不需要。比如:

  • 当一个事件比如保存文件结束时,应该出现一条消息确认保存成功。(Toast适用)
  • 如果一个后台运行的应用程序需要用户关注,这个应用程序应该创建一个通知来允许用户在方便时进行应答。(后台程序,状态栏通知适用)
  • 如果这个应用程序在执行一个用户必须等待的任务(比如加载一个文件),那么应用程序应该显示一个盘旋的进度轮或进度条。(进度条Dialog适用)

    所有这些通知任务可以通过一个不同的技术获取到:

  • 一个消息条通知Toast Notification, 用于从后台出现的简短信息。for brief messages that come from the background.
  • 一个状态条通知A Status Bar Notification, 用于来自后台的持续提醒并请求用户应答
  • 个对话框通知A Dialog Notification, 用于活动相关的通知
 

消息条通知Toast Notification

    一个消息条通知是一个在窗口表面弹出的信息。它只填充内容所需的空间并且用户当前活动仍然保持可见和可交互。这个通知自动渐入渐出,而且不接受交互事件。因为消息条可以从一个后台服务Service中创建,即便应用程序不可见,它也将呈现出来。

Toast     Toast

默认的Toast    自定义的Toast

Toast的创建和显示都很简单,如果不使用自定义的view,只是显示文字,makeText函数就能做到了:

Toast.makeText(getApplicationContext(),   // Context context
               "This is a simple toast!", //显示的text或者引用resource.string的id
               Toast.LENGTH_LONG)         //显示的时间长度,
                                          //LENGTH_LONG - 时间长些,>1s;
                                          //LENGTH_SHORT- 时间短
     .show(); //显示出来
自定义Toast

而如果想使用custom view,首先要写个custom Layout xml文件(toastlayout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="#DAAA"
    >
 
    <ImageView
        android:id="@+id/toast_icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:src="@drawable/icon"
        />
 
    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:textColor="#FFF"
        />
 
</LinearLayout>

注意: TextView的layout_height设置为wrap_content的话,发现左边的图片会跟随文字内容的高度而变化,就是说当文字只有一行的时候,图片的高度就变得只有一行的高度,不好看!图片的src可以不在xml文件里面定义,可以在真正显示时用以下语句来设置:

ImageView image = (ImageView) layout.findViewById(R.id.image); 
image.setImageResource(R.drawable.android);

以下是显示custom toast view:

//加载Layout
View view = getLayoutInflater().inflate(
        R.layout.toastlayout,                         //resource id
        (ViewGroup) findViewById(R.id.toast_layout)); //ViewGroup对象
 
//设置Text
((TextView) view.findViewById(R.id.toast_text))
        .setText("This is a custom toast!"); 
 
//创建Toast
Toast toast = new Toast(getApplicationContext());
 
// 设置显示的位置 
toast.setGravity( 
        Gravity.CENTER_VERTICAL, //垂直居中
        0, //xOffset
        0  //yOffset
);
 
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view); // ** 这个很重要
toast.show();

** 官方提示:只有使用custom view,setView(View)的时候,才使用new Toast(Content content)来得到Toast对象,否则必须用makeText(Context, int, int)方法来创建toast对象

消息条Toast是用来显示简短文本信息的最好方法,比如“文件已保存”,当你很确信用户正在关注屏幕时。一个消息条不能接受用户交互事件;如果你希望用户应答并采取相应动作,请考虑使用一个状态条通知Status Bar Notification。

状态条通知Status Bar Notification

    一个状态条通知添加一个图标到系统状态栏上(以及一个可选的滚动条文本信息)以及在这个“通知”窗口中的一个扩展消息。当用户选择这个扩展消息时,Android发出这个通知所定义的一个意图(通常是启动一个活动)。你也可以配置这个通知来通过一个声音,震动和设备上的闪烁灯来警告用户。

    当你的应用程序以后台服务运行并需要通知用户事件时,这类通知是一个理想的方式。如果你需要在活动仍处于焦点下时警告用户一个发生的事件,请考虑使用对话框通知Dialog Notification 。

notification

自定义View和默认的Notification

状态栏Notification的创建流程大概是这样的:

// 获取NotificationManager
mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 
// 创建PendingIntent, 明确响应后转向的Activity
contentIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                new Intent(getApplicationContext(),
                        FrameDemo.class), //响应Notification转向的Activity
                0); 
 
//实例化一个Notification,并指定其图标和标题(在提示栏上显示)
mNotification = new Notification(
    R.drawable.icon, // icon
    "Notification",  // 状态栏上显示的滚动提示文字tickerText
    System.currentTimeMillis());//Notification计划执行的开始时间
 
//设置Notification的Title和详细内容(打开提示栏后在通知列表中显示)
mNotification.setLatestEventInfo(
    getApplicationContext(),
    "Notification open", // Title
    "This is a simple notification", //content
    contentIntent);      //PendingIntent是在这时候用的
 
//100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
mNotification.vibrate = new long[] { 100, 250, 100, 500 };
//mNotification.defaults |= Notification.DEFAULT_VIBRATE; //或者设置默认的震动效果
 
//设置闪灯绿光,也可以设置默认的效果,请参考API DOC
mNotification.ledARGB = 0xff00ff00;
mNotification.ledOnMS = 300;
mNotification.ledOffMS = 1000;
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
 
//设置声音,可以选择以下任一方式,但要注意,当defaults已经包含了DEFAULT_SOUND,将覆盖之前指定的音频文件的设置
mNotification.defaults |= Notification.DEFAULT_SOUND; //系统默认的通知声音
//mNotification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //或者指定某个音频文件
 
//设置声音持续重复,直至用户点击或者清除通知
mNotification.flags |= Notification.FLAG_INSISTENT;
 
//最后一步: NotificationManager发出通知
mNotificationManager.notify(
        R.id.notice1,  //该Notification的ID
        mNotification);
清除通知Clear

    如果需要把Notification清除(clear),则调用NotificationManager.cancel(Notification的ID),或者直接NotificationManager.clearAll()清除所有。也可以添加Notification对象的FLAG_AUTO_CANCEL属性来自动清除。

更新通知Update the notification

    可以通过notification对象的setLatestEventInfo()方法来修改/更新信息,也可以通过其他函数来修改notification对象的属性,最后是调用NotificationManager.notify(原来的ID, 修改/更新后的notification对象)完成的。

//修改Title & content
mNotification.setLatestEventInfo(
    getApplicationContext(),
    "Notification update", // Title
    "This is the second notification", //content
    contentIntent);
 
//修改Icon
mNotification.icon = R.drawable.robot;
 
//设置自动清除
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
 
//重新发出通知
mNotificationManager.notify(R.id.notice1, mNotification);
自定义view的Notification

首先定义Layout xml文件(notifylayout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notify_layout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp"
    >
 
    <ImageView 
        android:id="@+id/notify_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        />
 
    <TextView 
        android:id="@+id/notify_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000"
        />
 
</LinearLayout>

 然后使用RemoteView加载Layout,并设置Image,Text:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notifylayout);
contentView.setImageViewResource(R.id.notify_image, R.drawable.robot);
contentView.setTextViewText(R.id.notify_text, "Hello, this message is in a custom expanded view");
 
Notification notification = new Notification(
    R.drawable.icon, //icon
    "Notification", // 状态栏上显示的提示文字
    System.currentTimeMillis());
notification.contentIntent = contentIntent;
notification.contentView = contentView; //就是这里不同,set view
 
// 以R.layout.notify_layout为ID
mNotificationManager.notify(R.layout.notifylayout, notification);

    官方提示,使用custom view的Notification要注意在不同规格的屏幕下的显示效果。个人认为,Notification的自定义View用途不大。更详细的内容可以查阅http://androidappdocs.appspot.com/guide/topics/ui/notifiers/notifications.html

 

对话框通知Dialog Notification

    一个对话框通常是出现在当前活动前面的一个小窗口。背后的活动丢失焦点而由这个对话框接受所有的用户交互。对话框通常用做和运行中应用程序直接相关的通知和短暂活动。

    你应该使用对话框来显示一个进度条或者一个需要用户确认的短消息(比如带有“确认”和“取消”按钮的一个警告)。你也可以把对话框作为构成应用程序界面整体的组件以及用于除了通知之外的其它目的。

    如何使用对话框通知,请参考对话框Dialoghttp://android.blog.51cto.com/268543/333769


本文转自 Icansoft 51CTO博客,原文链接: 

http://blog.51cto.com/android/333573
相关文章
|
21天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
25 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
101 0
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
42 1
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
10天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
13天前
|
XML 移动开发 Android开发
构建高效安卓应用:采用Jetpack Compose实现动态UI
【4月更文挑战第10天】 在现代移动开发中,用户界面的流畅性和响应性对于应用的成功至关重要。随着技术的不断进步,安卓开发者寻求更加高效和简洁的方式来构建动态且吸引人的UI。本文将深入探讨Jetpack Compose这一革新性技术,它通过声明式编程模型简化了UI构建过程,并提升了性能与跨平台开发的可行性。我们将从基本概念出发,逐步解析如何利用Jetpack Compose来创建具有数据动态绑定能力的安卓应用,同时确保应用的高性能和良好用户体验。
15 0
|
15天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
16天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
1月前
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
15 0
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4