在Android手机界面的最上方有一条显示时间、信号强度和电池状态等信息的区域,这就是Android的状态栏。当系统有一些信息要通知手机用户时,例如,收到新短信,电子邮件或未接来电时,系统通常会把信息显示在状态栏中,有的仅显示小图标,有的则显示图标及文字提醒,用手指按住状态栏往下拉,还可以展开状态栏,查看所有系统发出的信息。
在程序中,要如何把提示信息放入状态栏,又要如何显示小图标呢?Android API为了管理通知信息(Notification),定义了NotificationManager,只要调用它的Notify方法,即可将信息显示在状态栏。
先准备几张用于显示的图片,然后在界面上放一个Spinner,通过点击不同的选项,更换相应的图标。布局文件很简单:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Spinner android:id="@+id/mySpinnner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"></Spinner>
- </RelativeLayout>
- NotificationManager nm;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- try
- {
- nm = this.GetSystemService(Service.NotificationService) as NotificationManager;
- Spinner mySpinner = FindViewById<Spinner>(Resource.Id.mySpinnner);
- ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, new string[] { "显示图标1", "显示图标2", "显示图标3", "显示图标4", "关闭显示" });
- mySpinner.Adapter = adapter;
- mySpinner.ItemSelected += (sender, e) =>
- {
- switch (e.Position)
- {
- case 0:
- Notify(Resource.Drawable.btn1, "通知1");
- break;
- case 1:
- Notify(Resource.Drawable.btn2, "通知2");
- break;
- case 2:
- Notify(Resource.Drawable.btn3, "通知3");
- break;
- case 3:
- Notify(Resource.Drawable.btn4, "通知4");
- break;
- case 4:
- nm.Cancel(0);
- break;
- }
- };
- }
- catch (System.Exception ex)
- {
- MessageBox.ShowErrorMessage(this, ex);
- }
- }
- private void Notify(int iconId, string text)
- {
- Activity2 act2 = new Activity2();
- Intent notifyIntent = new Intent(this, act2.Class);
- notifyIntent.SetFlags(ActivityFlags.NewTask);
- PendingIntent appIntent = PendingIntent.GetActivity(this, 0, notifyIntent, 0);
- Notification n = new Notification
- {
- Icon = iconId,
- TickerText = new Java.Lang.String(text),
- Defaults = NotificationDefaults.Sound,
- };
- n.SetLatestEventInfo(this, "通知", text, appIntent);
- nm.Notify(0, n);
- }
在将状态栏拉下来单击通知的时候打开Activity2,在Activity2中,显示一个Toast。
- [Activity(Label = "My Activity")]
- public class Activity2 : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Activity2);
- Toast.MakeText(this, "由通知区域点击打开的", ToastLength.Short).Show();
- }
- }
运行效果:

发出Notification时,有三个地方可以显示文字,第一个是Notification被添加到状态栏时,跟着图标跑出的文字,在程序中以n.TickerText来设置;其余两个地方则是在Notification列表中显示的信息标题与信息内容,实在调用SetLatestEventInfo()方法是所设置的。如果在状态栏中不显示文字,只显示图片的话,只要不设置TickerText就可以了。
如果想要在通知显示的时候手机震动的话,我们就要使用Vibrator对象了。设置震动事件中,必须要知道命令其振动的时间长短、震动事件的周期等,而要在Android里设置的数值,都是以毫秒为计算的,所以在做设置的时候要注意下,如果设置的时间太小,会感觉不出来。
- Vibrator v = this.Application.GetSystemService(Service.VibratorService) as Vibrator;
- v.Vibrate(new long[] { 100, 10, 100, 1000 }, 0);
Vibrate方法的第一个参数是一个long数组,表示震动的频率,大家可以改变数组的长度及值来试试效果。第二个参数repeat,当等于0时,震动会一直持续,若等于-1,震动就会出现一轮,震动完毕就不会再有动作。
要使手机震动,必须要在AndroidManifest.xml中加入VIBRATE权限。
- <uses-permission android:name="android.permission.VIBRATE" />
记得在模拟器上是模拟不出震动的哦~~拷到手机里震一下吧~~
转载http://blog.youkuaiyun.com/ojlovecd/article/details/6362709