MonoDroid学习笔记(十二)—— 您有一条新短信息,请注意查收。状态栏提醒

在Android手机界面的最上方有一条显示时间、信号强度和电池状态等信息的区域,这就是Android的状态栏。当系统有一些信息要通知手机用户时,例如,收到新短信,电子邮件或未接来电时,系统通常会把信息显示在状态栏中,有的仅显示小图标,有的则显示图标及文字提醒,用手指按住状态栏往下拉,还可以展开状态栏,查看所有系统发出的信息。

在程序中,要如何把提示信息放入状态栏,又要如何显示小图标呢?Android API为了管理通知信息(Notification),定义了NotificationManager,只要调用它的Notify方法,即可将信息显示在状态栏。

先准备几张用于显示的图片,然后在界面上放一个Spinner,通过点击不同的选项,更换相应的图标。布局文件很简单:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"  
  4.                 android:layout_height="fill_parent">  
  5.   <Spinner android:id="@+id/mySpinnner"  
  6.            android:layout_width="fill_parent"  
  7.            android:layout_height="wrap_content"></Spinner>  
  8. </RelativeLayout>  
 

 

[c-sharp] view plain copy print ?
  1. NotificationManager nm;  
  2. protected override void OnCreate(Bundle bundle)  
  3. {  
  4.     base.OnCreate(bundle);  
  5.     SetContentView(Resource.Layout.Main);  
  6.     try  
  7.     {  
  8.         nm = this.GetSystemService(Service.NotificationService) as NotificationManager;  
  9.         Spinner mySpinner = FindViewById<Spinner>(Resource.Id.mySpinnner);  
  10.         ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, new string[] { "显示图标1""显示图标2""显示图标3""显示图标4""关闭显示" });  
  11.         mySpinner.Adapter = adapter;  
  12.         mySpinner.ItemSelected += (sender, e) =>  
  13.         {  
  14.             switch (e.Position)  
  15.             {  
  16.                 case 0:  
  17.                     Notify(Resource.Drawable.btn1, "通知1");  
  18.                     break;  
  19.                 case 1:  
  20.                     Notify(Resource.Drawable.btn2, "通知2");  
  21.                     break;  
  22.                 case 2:  
  23.                     Notify(Resource.Drawable.btn3, "通知3");  
  24.                     break;  
  25.                 case 3:  
  26.                     Notify(Resource.Drawable.btn4, "通知4");  
  27.                     break;  
  28.                 case 4:  
  29.                     nm.Cancel(0);  
  30.                     break;  
  31.             }  
  32.         };  
  33.     }  
  34.     catch (System.Exception ex)  
  35.     {  
  36.         MessageBox.ShowErrorMessage(this, ex);  
  37.     }  
  38. }  
  39. private void Notify(int iconId, string text)  
  40. {  
  41.     Activity2 act2 = new Activity2();  
  42.     Intent notifyIntent = new Intent(this, act2.Class);  
  43.     notifyIntent.SetFlags(ActivityFlags.NewTask);  
  44.     PendingIntent appIntent = PendingIntent.GetActivity(this, 0, notifyIntent, 0);  
  45.     Notification n = new Notification  
  46.     {  
  47.         Icon = iconId,  
  48.         TickerText = new Java.Lang.String(text),  
  49.         Defaults = NotificationDefaults.Sound,  
  50.     };  
  51.     n.SetLatestEventInfo(this"通知", text, appIntent);  
  52.     nm.Notify(0, n);  
  53. }  


在将状态栏拉下来单击通知的时候打开Activity2,在Activity2中,显示一个Toast。

[c-sharp]  view plain copy print ?
  1. [Activity(Label = "My Activity")]  
  2. public class Activity2 : Activity  
  3. {  
  4.     protected override void OnCreate(Bundle bundle)  
  5.     {  
  6.         base.OnCreate(bundle);  
  7.         SetContentView(Resource.Layout.Activity2);  
  8.         Toast.MakeText(this"由通知区域点击打开的", ToastLength.Short).Show();  
  9.     }  
  10. }  
 

 

运行效果:

 

发出Notification时,有三个地方可以显示文字,第一个是Notification被添加到状态栏时,跟着图标跑出的文字,在程序中以n.TickerText来设置;其余两个地方则是在Notification列表中显示的信息标题与信息内容,实在调用SetLatestEventInfo()方法是所设置的。如果在状态栏中不显示文字,只显示图片的话,只要不设置TickerText就可以了。

如果想要在通知显示的时候手机震动的话,我们就要使用Vibrator对象了。设置震动事件中,必须要知道命令其振动的时间长短、震动事件的周期等,而要在Android里设置的数值,都是以毫秒为计算的,所以在做设置的时候要注意下,如果设置的时间太小,会感觉不出来。

 

[c-sharp]  view plain copy print ?
  1. Vibrator v = this.Application.GetSystemService(Service.VibratorService) as Vibrator;  
  2. v.Vibrate(new long[] { 100, 10, 100, 1000 }, 0);  
 

 

Vibrate方法的第一个参数是一个long数组,表示震动的频率,大家可以改变数组的长度及值来试试效果。第二个参数repeat,当等于0时,震动会一直持续,若等于-1,震动就会出现一轮,震动完毕就不会再有动作。

要使手机震动,必须要在AndroidManifest.xml中加入VIBRATE权限。

[xhtml]  view plain copy print ?
  1. <uses-permission android:name="android.permission.VIBRATE" />  
 

记得在模拟器上是模拟不出震动的哦~~拷到手机里震一下吧~~

转载http://blog.csdn.net/ojlovecd/article/details/6362709

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值