Xamarin.Android 里面的通知

来源:互联网 发布:bytebuffer byte数组 编辑:程序博客网 时间:2024/05/22 01:50

我们每天都离不开手机App应用,也每天都接受各种消息通知,这几天自己刚好在研究Xamarin.Android 里面的通知,在此做个笔记总结,有什么理解不当之处请大家指正。

(1)首先在界面拖3个Button

(2)在MainActivity里面找到界面的对应Button元素(我设定三个Button的ID分别为(button1,button2,button3)

  Button StartButton = FindViewById<Button>(Resource.Id.button1);            Button EndButton = FindViewById<Button>(Resource.Id.button2);            Button VoiceButton = FindViewById<Button>(Resource.Id.button3);

(3)定义一个NotificationManager类型的变量,我取名为notification

 private NotificationManager notification;

(4)获取管理通知类,在Xamarin.Android下的通知需要获取NotificationManager服务,而该服务需要通过GetSystemService获取

 <span style="font-size:18px;">//获得系统管理类</span>            notification = (NotificationManager) GetSystemService(NotificationService);

(5)为三个button分别挂载事件

   //开始通知            StartButton.Click += delegate            {                //设置通知的图标以及显示的简介Title                Notification notify = new Notification(Resource.Drawable.Icon, "普通通知");                //初始化点击通知后打开的活动,我们点击通知之后都会打开对应的活动,所以我们需要初始化一个延迟意图,以便通知可以打开                PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent);                //设置通知的主体                notify.SetLatestEventInfo(this, "通知李白", "你二级没过", pintent);                //发送通知                notification.Notify(0, notify);//0为该通知的ID,方便后面接收该通知            };
在取消通知里面,notification里面的Cancel方法里面的参数是通知ID,是根据ID来区分究竟应该取消哪一个通知,或者你也可以选择All,进而选择取消全部通知
  //取消通知            EndButton.Click += delegate            {                //根据ID取消通知                notification.Cancel(0);                notification.Cancel(1);            };
  //带有震动,声音的通知            VoiceButton.Click += delegate            {                Notification notify = new Notification(Resource.Drawable.Icon, "带有声音、LED光和震动的通知");                //设置该通知具有声音、LED光和震动,即所有                notify.Defaults = NotificationDefaults.All;                //获取系统默认的通知声音                Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);                //设置通知的声音                notify.Sound = ringUri;                //设置一秒的震动                notify.Vibrate = new long[] { 1000 };                //设置LED的颜色为绿色                notify.LedARGB = Color.Green;                //设置LED显示时间为1s                notify.LedOnMS = 1000;                //设置LED熄灭时间为1s                notify.LedOffMS = 1000;                //设置标志位,否则无法显示LED                notify.Flags = NotificationFlags.ShowLights | notify.Flags;                PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0);                notify.SetLatestEventInfo(this, "通知璐璐", "回来吃饺子", pintent);                notification.Notify(1, notify);            };
最后打开Genymotion模拟器,运行一下



好了,大功告成!继续,革命尚未成功,“通知”仍需努力。


0 0
原创粉丝点击