>>分享Android开发相关的技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 18988 个阅读者 刷新本主题
 * 贴子主题:  Android UI学习 - 用户通知-学习Android 回复文章 点赞(0)  收藏  
作者:Jacky    发表时间:2020-05-15 05:55:58     消息  查看  搜索  好友  邮件  复制  引用

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

通知用户Notifying the User

         某些情况下需要通知用户你的应用程序中发生了一个事件。一些事件请求用户应答而另外一些则不需要。比如:    
  • 当一个事件比如保存文件结束时,应该出现一条消息确认保存成功。(<u>Toast适用</u>)
  • 如果一个后台运行的应用程序需要用户关注,这个应用程序应该创建一个通知来允许用户在方便时进行应答。(<u>后台程序,状态栏通知适用</u>)
  • 如果这个应用程序在执行一个用户必须等待的任务(比如加载一个文件),那么应用程序应该显示一个盘旋的进度轮或进度条。(<u>进度条Dialog适用</u>)
         所有这些通知任务可以通过一个不同的技术获取到:    
  • 一个消息条通知Toast Notification, 用于从后台出现的简短信息。for brief messages that come from the background.
  • 一个状态条通知A Status Bar Notification, 用于来自后台的持续提醒并请求用户应答
  • 个对话框通知A Dialog Notification, 用于活动相关的通知

消息条通知Toast Notification

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

         点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小       点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

         默认的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>

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

ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath);
p_w_picpath.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();

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

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

状态条通知Status Bar Notification

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

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

         点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

         自定义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_p_w_picpath"
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_p_w_picpath, 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



----------------------------
原文链接:https://blog.51cto.com/android/333573

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-06-05 08:44:04 重新编辑]
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=3243

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员