练习Toast和Notification
来源:互联网 发布:树莓派3 ubuntu 16.04 编辑:程序博客网 时间:2024/06/05 00:31
5.7.1 Toast提醒你
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为大家介绍Toast提醒你。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7 Toast和Notification控件实现提醒
生活中有提醒,Android中也有提醒。在Android中,可以通过Toast和Notification控件来实现提醒功能。和Dialog相比,此类型提醒更加友好和温馨,并且不会打断用户的当前操作。在本节内容中将详细讲解Toast和Notification控件的具体使用方法。
5.7.1 Toast提醒你
Toast是Android 中用来显示信息的一种机制,和Dialog不同的是,Toast没有焦点,而且Toast 显示的时间有限,经过一定的时间后就会自动消失。
5.7.2 Notification提醒你
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为大家介绍Notification提醒你。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7.2 Notification提醒你
看名字就知道,Notification和提醒有关,它通常和NotificationManager同时使用。具体来说,其主要功能如下:
1.NotificationManager和Notification用来设置通知
通知的设置等操作相对比较简单,基本的使用方式就是用新建一个Notification对象,然后设置好通知的各项参数,然后使用系统后台运行的NotificationManager服务将通知发出来。基本步骤如下:
(1)得到NotificationManager:
(2)创建一个新的Notification对象:
- String ns = Context.NOTIFICATION_SERVICE;
- NotificationManager mNotificationManager = (NotificationManager)
- getSystemService(ns);
也可以使用稍微复杂一些的方式创建Notification:
- Notification notification = new Notification();
- notification.icon = R.drawable.notification_icon;
(3)填充Notification的各个属性:
- int icon = R.drawable.notification_icon; //通知图标
- CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示
- long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
- Notification notification = new Notification(icon, tickerText, when);
- Context context = getApplicationContext();
- CharSequence contentTitle = "My notification";
- CharSequence contentText = "Hello World!";
- Intent notificationIntent = new Intent(this, MyClass.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- notificationIntent, 0);
- notification.setLatestEventInfo(context, contentTitle, contentText,
- contentIntent);
Notification提供了如下几种手机提示方式:
在状态栏(Status Bar)显示的通知文本提示,例如:notification.tickerText = "hello";
发出提示音,例如:
手机振动,例如:
- notification.defaults |= Notification.DEFAULT_SOUND;
- notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
- notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_
- URI, "6");
LED灯闪烁,例如:
- notification.defaults |= Notification.DEFAULT_VIBRATE;
- long[] vibrate = {0,100,200,300};
- notification.vibrate = vibrate;
(4)发送通知,代码如下:
- notification.defaults |= Notification.DEFAULT_LIGHTS;
- notification.ledARGB = 0xff00ff00;
- notification.ledOnMS = 300;
- notification.ledOffMS = 1000;
- notification.flags |= Notification.FLAG_SHOW_LIGHTS;
- private static final int ID_NOTIFICATION = 1;
- mNotificationManager.notify(ID_NOTIFICATION, notification);
2.更新通知
如果需要更新一个通知,则只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。
为了更新一个已经触发过的Not i f icat ion,传入相同的ID,此时可以传入相同的Notification对象,也可以是一个全新的对象。只要ID相同,新的Notification对象会替换状态条图标和扩展的状态窗口的细节。
另外,还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,例如:
当取消一个Notification时,会移除它的状态条图标以及清除在扩展的状态窗口中的信息。
- notificationManager.cancel(notificationRef);
5.7.3 练习Toast和Notification(1)
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为练习Toast和Notification。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7.3 练习Toast和Notification(1)
练习12:使用Toast和Notification实现提醒功能效果的的具体使用流程源码路径:“第5章\widges”文件夹
(1)新建一个Android工程文件,然后编写main.xml布局文件,具体代码如下所示:
通过上述代码插入了两个Button按钮,执行后的效果如图5-40所示。 图5-40 插入两个Button(2)编写处理文件ActivityMain.java,具体代码如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
- Android:orientation="vertical" Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <Button Android:id="@+id/button1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="介绍Notification"
- />
- <Button Android:id="@+id/button2"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="介绍Toast" />
- </LinearLayout>
- public class ActivityMain extends Activity {
- OnClickListener listener1 = null;
- OnClickListener listener2 = null;
- Button button1;
- Button button2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listener1 = new OnClickListener() {
- public void onClick(View v) {
- setTitle(“这是Notification");
- Intent intent = new Intent(ActivityMain.this,
- ActivityMainNotification.class);
- startActivity(intent);
- }
- };
- listener2 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("这是Toast");
- Intent intent = new Intent(ActivityMain.this,
- ActivityToast.class);
- startActivity(intent);
- }
- };
- setContentView(R.layout.main);
- button1 = (Button) findViewById(R.id.button1);
- button1.setOnClickListener(listener1);
- button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(listener2);
- }
- }
在上述代码中,对两个Button绑定了单击监听器OnClickListener,当单击这两个Button时,会跳转到新的Activity上。
(3)编写第一个Button的处理程序,即单击图5-21中的“介绍Notification”按钮后,执行ActivityMainNotification.java,其主要代码如下所示:
- public class ActivityMainNotification extends Activity {
- private static int NOTIFICATIONS_ID = R.layout.activity_notification;
- private NotificationManager mNotificationManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_notification);
- Button button;
- mNotificationManager = (NotificationManager) getSystemService
- (NOTIFICATION_ SERVICE);
- button = (Button) findViewById(R.id.sun_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("好", "天气", "好", R.drawable.sun);
- }
- });
- button = (Button) findViewById(R.id.cloudy_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("还行", "天气", "还行", R.drawable.cloudy);
- }
- });
- button = (Button) findViewById(R.id.rain_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("不好", "天气", "不好", R.drawable.rain);
- }
- });
- button = (Button) findViewById(R.id.defaultSound);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_SOUND);
- }
- });
- button = (Button) findViewById(R.id.defaultVibrate);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_VIBRATE);
- }
- });
- button = (Button) findViewById(R.id.defaultAll);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_ALL);
- }
- });
- button = (Button) findViewById(R.id.clear);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- mNotificationManager.cancel(NOTIFICATIONS_ID);
- }
- });
- }
- private void setWeather(String tickerText, String title, String content,
- int drawable) {
- Notification notification = new Notification(drawable, tickerText,
- System.currentTimeMillis());
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- notification.setLatestEventInfo(this, title, content, contentIntent);
- mNotificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- private void setDefault(int defaults) {
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- String title = "天气预报";
- String content = "晴";
- final Notification notification = new Notification(R.drawable.sun,
- content, System.currentTimeMillis());
- notification.setLatestEventInfo(this, title, content, contentIntent);
- notification.defaults = defaults;
- mNotificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- }
5.7.3 练习Toast和Notification(2)
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为练习Toast和Notification。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7.3 练习Toast和Notification(2)
因为所有的Notification都是通过NotificationManager来管理的,所以应该首先得到NotificationManager实例,以便管理这个Activity中的跳转服务。获取NotificationManager实例的代码如下所示:
mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_
SERVICE);
函数setWeather是ActivityMainNotification中的一个重要函数,它实例化了一个Notification,并将这个Notification显示出来。在下面的代码中:
Notification notification = new Notification(drawable, tickerText,
System.currentTimeMillis());
包含了3个参数,具体说明如下:
第1个:要显示的图片的ID;
第2个:显示的文本文字;
第3个: N o t i f i c a t i o n 显示的时间, 一般是立即显示, 时间就是S y s t e m .currentTimeMillis()。
函数setDefault也是ActivityMainNotification.java中的一个重要函数,在此函数中初始化了一个Notification,在设置Notification时使用了其默认值的形式,即:
notification.defaults = defaults;
另外,在上述程序中还用到了以下几种表现形式:
Notification.DEFAULT_VIBRATE:表示当前的Notification
显示出来时手机会发出震动;
Notification.DEFAULT_SOUND:表示当前的Notification显示出来时手机会伴随音乐;
Notification.DEFAULT_ALL:表示当前的Notification显示出来时手机既会震动,也会伴随音乐。
这样当单击第一个Button后会执行上述处理程序,来到对应的新界面,新界面的布局文件是由activity_notification.xml实现的,其主要代码如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <Button
- Android:id="@+id/sun_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="适合" />
- <Button
- Android:id="@+id/cloudy_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="一般" />
- <Button
- Android:id="@+id/rain_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="一点也不适合" />
- </LinearLayout>
- <TextView
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_marginTop="20dip"
- Android:text="高级notification" />
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <Button
- Android:id="@+id/defaultSound"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="有声音的提示" />
- <Button
- Android:id="@+id/defaultVibrate"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="振动的提示" />
- <Button
- Android:id="@+id/defaultAll"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="声音+振动的提示" />
- </LinearLayout>
- <Button Android:id="@+id/clear"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_marginTop="20dip"
- Android:text="清除提示" />
- </LinearLayout>
- </ScrollView>
5.7.3 练习Toast和Notification(3)
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为大家介绍练习Toast和Notification。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7.3 练习Toast和Notification(3)
程序执行后新界面的效果如图5-41所示。
当单击图5-41所示的新界面中的Button后,会根据上述处理文件而实现某种效果,例如,单击“有声音的提示”按钮后,会发出声音。
(4)编写第二个Button的处理程序,即单击图5-41中的“振动的提示”按钮后,执行ActivityToast.java,其主要代码如下所示:
图5-41 运行效果
- public class ActivityToast extends Activity {
- OnClickListener listener1 = null;
- OnClickListener listener2 = null;
- Button button1;
- Button button2;
- private static int NOTIFICATIONS_ID = R.layout.activity_toast;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listener1 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("短时提醒");
- showToast(Toast.LENGTH_SHORT);
- }
- };
- listener2 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("长时提醒");
- showToast(Toast.LENGTH_LONG);
- showNotification();
- }
- };
- setContentView(R.layout.activity_toast);
- button1 = (Button) findViewById(R.id.button1);
- button1.setOnClickListener(listener1);
- button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(listener2);
- }
- protected void showToast(int type) {
- View view = inflateView(R.layout.toast);
- TextView tv = (TextView) view.findViewById(R.id.content);
- tv.setText("欢迎来到济南,");
- /*实例化Toast*/
- Toast toast = new Toast(this);
- toast.setView(view);
- toast.setDuration(type);
- toast.show();
- }
- private View inflateView(int resource) {
- LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_
- INFLATER_SERVICE);
- return vi.inflate(resource, null);
- }
- protected void showNotification() {
- NotificationManager notificationManager = (NotificationManager)
- getSystemService(NOTIFICATION_SERVICE);
- CharSequence title = "省会";
- CharSequence contents = "齐鲁大地";
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- Notification notification = new Notification(R.drawable.default_icon,
- title, System.currentTimeMillis());
- notification.setLatestEventInfo(this, title, contents, contentIntent);
- // 100ms延迟后振动250ms,停止100ms后振动500ms
- notification.vibrate = new long[] { 100, 250, 100, 500 };
- notificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- }
5.7.3 练习Toast和Notification(4)
综合评级:
想读(0) 在读(0) 已读(0) 品书斋鉴(0) 已有0人发表书评
《Android江湖》第5章系出名门,在本章内容中讲解了Android中基本控件的基本使用知识。首先介绍了UI等常用布局控件的基本知识和用法,然后详细讲解了友好界面控件和列表控件的的基本知识,最后讲解了Intent、Activity、Toast和Notification控件的基本知识。本节为大家介绍练习Toast和Notification。
AD:51CTO云计算架构师峰会 抢票进行中!
5.7.3 练习Toast和Notification(4)
上述代码是通过Toast实现的,它不需要用NotificationManager来管理,上述代码的处理流程如下:
实例化Toast,每个Toast和一个View相关;
设置Toast的长短,通过“showToast(Toast.LENGTH_SHORT);”来设置Toast短时间显示,通过“showToast(Toast.LENGTH_LONG);”来设置Toast长时间显示;
通过函数showToast来显示Toast,当单击“长时显示”按钮后,程序会长时间显示提醒,并用Notification在状态栏中提示用户;当单击“短时显示”按钮后,程序会短时间显示提醒。
这样单击第二个Button后会执行上述处理程序,来到对应的新界面,新界面的布局文件是由activity_toast.xml实现的,其主要代码如下所示:
程序执行后新界面的效果如图5-42所示。 图5-42 新界面的效果当单击图5-42所示的新界面中的Button后,会根据上述处理文件而实现某种效果,例如,单击“短时Toast”按钮后,会短时间显示一个提醒;当单击“长时Toast”按钮后,会长时间显示一个提醒。这两种方式的提醒界面都是相同的,具体效果如图5-43所示。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
- Android:orientation="vertical" Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <Button Android:id="@+id/button1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="短时Toast" />
- <Button Android:id="@+id/button2"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="长时Toast" />
- </LinearLayout>
- 练习Toast和Notification
- Toast和Notification
- Toast和Notification
- 自定义Notification和Toast
- Android-Toast和Notification
- Android中的Toast和Notification
- Toast Notification
- 4 代码引用(notification和Toast)
- 针对于Dialog来说Toast和Notification
- Android自定义Dialog,Toast,Notification和PopupWindow
- Android中的消息通知Toast和Notification
- Android中的消息通知Toast和Notification
- Toast消息提示和Notification通知
- Android中的消息通知Toast和Notification
- Toast(弹出信息)和Notification(下拉菜单通知)
- 【Android知识点精讲】(13)Toast和Notification
- 【wpf】在win10系统上弹出toast和notification
- Android Notification与Toast
- 典型sql面试点-rowid删除数据
- 电脑端口大全
- 如何使Windows Phone震动并播放声音
- ServletContext应用
- 总结网站优化的几个要点
- 练习Toast和Notification
- 文件批量重命名代码
- win7 隐藏任务栏操作中心图标
- 时间管理(Time Management)
- 59 斜体字加粗字
- ArcEngine如何获取mxd中feature所在图层的渲染方式
- OpenGL for Windows NT及其程序设计
- ORACLE 10g oracleDBConsoleocl服务无法启动 更改计算机名后
- make menuconfig显示错误“Your display is too small to run Menuconfig!”