练习Toast和Notification

来源:互联网 发布:树莓派3 ubuntu 16.04 编辑:程序博客网 时间:2024/06/05 00:31

5.7.1 Toast提醒你

2011-12-30 11:34 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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提醒你

2011-12-30 11:34 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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:

  1. String ns = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager mNotificationManager = (NotificationManager)  
  3. getSystemService(ns); 

(2)创建一个新的Notification对象:
  1. Notification notification = new Notification();  
  2. notification.icon = R.drawable.notification_icon; 

也可以使用稍微复杂一些的方式创建Notification:

  1. int icon = R.drawable.notification_icon; //通知图标  
  2. CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示  
  3. long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示  
  4. Notification notification = new Notification(icon, tickerText, when); 

(3)填充Notification的各个属性:
  1. Context context = getApplicationContext();  
  2. CharSequence contentTitle = "My notification";  
  3. CharSequence contentText = "Hello World!";  
  4. Intent notificationIntent = new Intent(this, MyClass.class);  
  5. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  6. notificationIntent, 0);  
  7. notification.setLatestEventInfo(context, contentTitle, contentText,  
  8. contentIntent); 

Notification提供了如下几种手机提示方式:

在状态栏(Status Bar)显示的通知文本提示,例如:notification.tickerText = "hello";

发出提示音,例如:

  1. notification.defaults |= Notification.DEFAULT_SOUND;  
  2. notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");  
  3. notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_  
  4. URI, "6"); 

手机振动,例如:
  1. notification.defaults |= Notification.DEFAULT_VIBRATE;  
  2. long[] vibrate = {0,100,200,300};  
  3. notification.vibrate = vibrate; 

LED灯闪烁,例如:
  1. notification.defaults |= Notification.DEFAULT_LIGHTS;  
  2. notification.ledARGB = 0xff00ff00;  
  3. notification.ledOnMS = 300;  
  4. notification.ledOffMS = 1000;  
  5. notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

(4)发送通知,代码如下:

  1. private static final int ID_NOTIFICATION = 1;  
  2. mNotificationManager.notify(ID_NOTIFICATION, notification); 

2.更新通知

如果需要更新一个通知,则只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。

为了更新一个已经触发过的Not i f icat ion,传入相同的ID,此时可以传入相同的Notification对象,也可以是一个全新的对象。只要ID相同,新的Notification对象会替换状态条图标和扩展的状态窗口的细节。

另外,还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,例如:

  1. notificationManager.cancel(notificationRef); 

当取消一个Notification时,会移除它的状态条图标以及清除在扩展的状态窗口中的信息。


5.7.3 练习Toast和Notification(1)

2011-12-30 11:34 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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布局文件,具体代码如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:orientation="vertical" Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <Button Android:id="@+id/button1" 
  6. Android:layout_width="wrap_content" 
  7. Android:layout_height="wrap_content" Android:text="介绍Notification" 
  8. /> 
  9. <Button Android:id="@+id/button2" 
  10. Android:layout_width="wrap_content" 
  11. Android:layout_height="wrap_content" Android:text="介绍Toast" /> 
  12. </LinearLayout> 

通过上述代码插入了两个Button按钮,执行后的效果如图5-40所示。 图5-40 插入两个Button(2)编写处理文件ActivityMain.java,具体代码如下所示:
  1. public class ActivityMain extends Activity {  
  2. OnClickListener listener1 = null;  
  3. OnClickListener listener2 = null;  
  4. Button button1;  
  5. Button button2;  
  6. @Override  
  7. public void onCreate(Bundle savedInstanceState) {  
  8. super.onCreate(savedInstanceState);  
  9. listener1 = new OnClickListener() {  
  10. public void onClick(View v) {  
  11. setTitle(“这是Notification");  
  12. Intent intent = new Intent(ActivityMain.this,  
  13. ActivityMainNotification.class);  
  14. startActivity(intent);  
  15. }  
  16. };  
  17. listener2 = new OnClickListener() {  
  18. public void onClick(View v) {  
  19. setTitle("这是Toast");  
  20. Intent intent = new Intent(ActivityMain.this,  
  21. ActivityToast.class);  
  22. startActivity(intent);  
  23. }  
  24. };  
  25. setContentView(R.layout.main);  
  26. button1 = (Button) findViewById(R.id.button1);  
  27. button1.setOnClickListener(listener1);  
  28. button2 = (Button) findViewById(R.id.button2);  
  29. button2.setOnClickListener(listener2);  
  30. }  

在上述代码中,对两个Button绑定了单击监听器OnClickListener,当单击这两个Button时,会跳转到新的Activity上。

(3)编写第一个Button的处理程序,即单击图5-21中的“介绍Notification”按钮后,执行ActivityMainNotification.java,其主要代码如下所示:

  1. public class ActivityMainNotification extends Activity {  
  2. private static int NOTIFICATIONS_ID = R.layout.activity_notification;  
  3. private NotificationManager mNotificationManager;  
  4. @Override  
  5. public void onCreate(Bundle savedInstanceState) {  
  6. super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.activity_notification);  
  8. Button button;  
  9. mNotificationManager = (NotificationManager) getSystemService  
  10. (NOTIFICATION_ SERVICE);  
  11. button = (Button) findViewById(R.id.sun_1);  
  12. button.setOnClickListener(new Button.OnClickListener() {  
  13. public void onClick(View v) {  
  14. setWeather("好", "天气", "好", R.drawable.sun);  
  15. }  
  16. });  
  17. button = (Button) findViewById(R.id.cloudy_1);  
  18. button.setOnClickListener(new Button.OnClickListener() {  
  19. public void onClick(View v) {  
  20. setWeather("还行", "天气", "还行", R.drawable.cloudy);  
  21. }  
  22. });  
  23. button = (Button) findViewById(R.id.rain_1);  
  24. button.setOnClickListener(new Button.OnClickListener() {  
  25. public void onClick(View v) {  
  26. setWeather("不好", "天气", "不好", R.drawable.rain);  
  27. }  
  28. });  
  29. button = (Button) findViewById(R.id.defaultSound);  
  30. button.setOnClickListener(new Button.OnClickListener() {  
  31. public void onClick(View v) {  
  32. setDefault(Notification.DEFAULT_SOUND);  
  33. }  
  34. });  
  35. button = (Button) findViewById(R.id.defaultVibrate);  
  36. button.setOnClickListener(new Button.OnClickListener() {  
  37. public void onClick(View v) {  
  38. setDefault(Notification.DEFAULT_VIBRATE);  
  39. }  
  40. });  
  41. button = (Button) findViewById(R.id.defaultAll);  
  42. button.setOnClickListener(new Button.OnClickListener() {  
  43. public void onClick(View v) {  
  44. setDefault(Notification.DEFAULT_ALL);  
  45. }  
  46. });  
  47. button = (Button) findViewById(R.id.clear);  
  48. button.setOnClickListener(new Button.OnClickListener() {  
  49. public void onClick(View v) {  
  50. mNotificationManager.cancel(NOTIFICATIONS_ID);  
  51. }  
  52. });  
  53. }  
  54. private void setWeather(String tickerText, String title, String content,  
  55. int drawable) {  
  56. Notification notification = new Notification(drawable, tickerText,  
  57. System.currentTimeMillis());  
  58. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  59. new Intent(this, ActivityMain.class), 0);  
  60. notification.setLatestEventInfo(this, title, content, contentIntent);  
  61. mNotificationManager.notify(NOTIFICATIONS_ID, notification);  
  62. }  
  63. private void setDefault(int defaults) {  
  64. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  65. new Intent(this, ActivityMain.class), 0);  
  66. String title = "天气预报";  
  67. String content = "晴";  
  68. final Notification notification = new Notification(R.drawable.sun,  
  69. content, System.currentTimeMillis());  
  70. notification.setLatestEventInfo(this, title, content, contentIntent);  
  71. notification.defaults = defaults;  
  72. mNotificationManager.notify(NOTIFICATIONS_ID, notification);  
  73. }  


5.7.3 练习Toast和Notification(2)

2011-12-30 11:34 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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实现的,其主要代码如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <LinearLayout 
  6. Android:orientation="vertical" 
  7. Android:layout_width="fill_parent" 
  8. Android:layout_height="wrap_content"> 
  9. <LinearLayout 
  10. Android:orientation="vertical" 
  11. Android:layout_width="fill_parent" 
  12. Android:layout_height="wrap_content"> 
  13. <Button 
  14. Android:id="@+id/sun_1" 
  15. Android:layout_width="wrap_content" 
  16. Android:layout_height="wrap_content" 
  17. Android:text="适合" /> 
  18. <Button 
  19. Android:id="@+id/cloudy_1" 
  20. Android:layout_width="wrap_content" 
  21. Android:layout_height="wrap_content" 
  22. Android:text="一般" /> 
  23. <Button 
  24. Android:id="@+id/rain_1" 
  25. Android:layout_width="wrap_content" 
  26. Android:layout_height="wrap_content" 
  27. Android:text="一点也不适合" /> 
  28. </LinearLayout> 
  29. <TextView 
  30. Android:layout_width="wrap_content" 
  31. Android:layout_height="wrap_content" 
  32. Android:layout_marginTop="20dip" 
  33. Android:text="高级notification" /> 
  34. <LinearLayout 
  35. Android:orientation="vertical" 
  36. Android:layout_width="fill_parent" 
  37. Android:layout_height="wrap_content"> 
  38. <Button 
  39. Android:id="@+id/defaultSound" 
  40. Android:layout_width="wrap_content" 
  41. Android:layout_height="wrap_content" 
  42. Android:text="有声音的提示" /> 
  43. <Button 
  44. Android:id="@+id/defaultVibrate" 
  45. Android:layout_width="wrap_content" 
  46. Android:layout_height="wrap_content" 
  47. Android:text="振动的提示" /> 
  48. <Button 
  49. Android:id="@+id/defaultAll" 
  50. Android:layout_width="wrap_content" 
  51. Android:layout_height="wrap_content" 
  52. Android:text="声音+振动的提示" /> 
  53. </LinearLayout> 
  54. <Button Android:id="@+id/clear" 
  55. Android:layout_width="wrap_content" 
  56. Android:layout_height="wrap_content" 
  57. Android:layout_marginTop="20dip" 
  58. Android:text="清除提示" /> 
  59. </LinearLayout> 
  60. </ScrollView> 

5.7.3 练习Toast和Notification(3)

2011-12-30 13:44 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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 运行效果

  1. public class ActivityToast extends Activity {  
  2. OnClickListener listener1 = null;  
  3. OnClickListener listener2 = null;  
  4. Button button1;  
  5. Button button2;  
  6. private static int NOTIFICATIONS_ID = R.layout.activity_toast;  
  7. @Override  
  8. public void onCreate(Bundle savedInstanceState) {  
  9. super.onCreate(savedInstanceState);  
  10. listener1 = new OnClickListener() {  
  11. public void onClick(View v) {  
  12. setTitle("短时提醒");  
  13. showToast(Toast.LENGTH_SHORT);  
  14. }  
  15. };  
  16. listener2 = new OnClickListener() {  
  17. public void onClick(View v) {  
  18. setTitle("长时提醒");  
  19. showToast(Toast.LENGTH_LONG);  
  20. showNotification();  
  21. }  
  22. };  
  23. setContentView(R.layout.activity_toast);  
  24. button1 = (Button) findViewById(R.id.button1);  
  25. button1.setOnClickListener(listener1);  
  26. button2 = (Button) findViewById(R.id.button2);  
  27. button2.setOnClickListener(listener2);  
  28. }  
  29. protected void showToast(int type) {  
  30. View view = inflateView(R.layout.toast);  
  31. TextView tv = (TextView) view.findViewById(R.id.content);  
  32. tv.setText("欢迎来到济南,");  
  33. /*实例化Toast*/  
  34. Toast toast = new Toast(this);  
  35. toast.setView(view);  
  36. toast.setDuration(type);  
  37. toast.show();  
  38. }  
  39. private View inflateView(int resource) {  
  40. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_  
  41. INFLATER_SERVICE);  
  42. return vi.inflate(resource, null);  
  43. }  
  44. protected void showNotification() {  
  45. NotificationManager notificationManager = (NotificationManager)  
  46. getSystemService(NOTIFICATION_SERVICE);  
  47. CharSequence title = "省会";  
  48. CharSequence contents = "齐鲁大地";  
  49. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  50. new Intent(this, ActivityMain.class), 0);  
  51. Notification notification = new Notification(R.drawable.default_icon,  
  52. title, System.currentTimeMillis());  
  53. notification.setLatestEventInfo(this, title, contents, contentIntent);  
  54. // 100ms延迟后振动250ms,停止100ms后振动500ms  
  55. notification.vibrate = new long[] { 100, 250, 100, 500 };  
  56. notificationManager.notify(NOTIFICATIONS_ID, notification);  
  57. }  

5.7.3 练习Toast和Notification(4)

2011-12-30 13:44 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(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实现的,其主要代码如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:orientation="vertical" Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <Button Android:id="@+id/button1" 
  6. Android:layout_width="wrap_content" 
  7. Android:layout_height="wrap_content" Android:text="短时Toast" /> 
  8. <Button Android:id="@+id/button2" 
  9. Android:layout_width="wrap_content" 
  10. Android:layout_height="wrap_content" Android:text="长时Toast" /> 
  11. </LinearLayout> 

程序执行后新界面的效果如图5-42所示。 图5-42 新界面的效果当单击图5-42所示的新界面中的Button后,会根据上述处理文件而实现某种效果,例如,单击“短时Toast”按钮后,会短时间显示一个提醒;当单击“长时Toast”按钮后,会长时间显示一个提醒。这两种方式的提醒界面都是相同的,具体效果如图5-43所示。

 


原创粉丝点击