MonoDroid学习笔记(十二)—— 您有一条新短信息,请注意查收。状态栏提醒
2013-06-13 16:54
274 查看
在Android手机界面的最上方有一条显示时间、信号强度和电池状态等信息的区域,这就是Android的状态栏。当系统有一些信息要通知手机用户时,例如,收到新短信,电子邮件或未接来电时,系统通常会把信息显示在状态栏中,有的仅显示小图标,有的则显示图标及文字提醒,用手指按住状态栏往下拉,还可以展开状态栏,查看所有系统发出的信息。
在程序中,要如何把提示信息放入状态栏,又要如何显示小图标呢?Android API为了管理通知信息(Notification),定义了NotificationManager,只要调用它的Notify方法,即可将信息显示在状态栏。
先准备几张用于显示的图片,然后在界面上放一个Spinner,通过点击不同的选项,更换相应的图标。布局文件很简单:
[xhtml] view
plaincopyprint?
<?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>
[c-sharp]
view plaincopyprint?
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。
[c-sharp] view
plaincopyprint?
[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();
}
}
运行效果:
![](http://hi.csdn.net/attachment/201104/25/4505_1303738857qo0g.png)
发出Notification时,有三个地方可以显示文字,第一个是Notification被添加到状态栏时,跟着图标跑出的文字,在程序中以n.TickerText来设置;其余两个地方则是在Notification列表中显示的信息标题与信息内容,实在调用SetLatestEventInfo()方法是所设置的。如果在状态栏中不显示文字,只显示图片的话,只要不设置TickerText就可以了。
如果想要在通知显示的时候手机震动的话,我们就要使用Vibrator对象了。设置震动事件中,必须要知道命令其振动的时间长短、震动事件的周期等,而要在Android里设置的数值,都是以毫秒为计算的,所以在做设置的时候要注意下,如果设置的时间太小,会感觉不出来。
[c-sharp] view
plaincopyprint?
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权限。
[xhtml] view
plaincopyprint?
<uses-permission android:name="android.permission.VIBRATE" />
记得在模拟器上是模拟不出震动的哦~~拷到手机里震一下吧~~
转载http://blog.csdn.net/ojlovecd/article/details/6362709
在程序中,要如何把提示信息放入状态栏,又要如何显示小图标呢?Android API为了管理通知信息(Notification),定义了NotificationManager,只要调用它的Notify方法,即可将信息显示在状态栏。
先准备几张用于显示的图片,然后在界面上放一个Spinner,通过点击不同的选项,更换相应的图标。布局文件很简单:
[xhtml] view
plaincopyprint?
<?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>
[c-sharp]
view plaincopyprint?
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。
[c-sharp] view
plaincopyprint?
[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();
}
}
运行效果:
![](http://hi.csdn.net/attachment/201104/25/4505_1303738857qo0g.png)
发出Notification时,有三个地方可以显示文字,第一个是Notification被添加到状态栏时,跟着图标跑出的文字,在程序中以n.TickerText来设置;其余两个地方则是在Notification列表中显示的信息标题与信息内容,实在调用SetLatestEventInfo()方法是所设置的。如果在状态栏中不显示文字,只显示图片的话,只要不设置TickerText就可以了。
如果想要在通知显示的时候手机震动的话,我们就要使用Vibrator对象了。设置震动事件中,必须要知道命令其振动的时间长短、震动事件的周期等,而要在Android里设置的数值,都是以毫秒为计算的,所以在做设置的时候要注意下,如果设置的时间太小,会感觉不出来。
[c-sharp] view
plaincopyprint?
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权限。
[xhtml] view
plaincopyprint?
<uses-permission android:name="android.permission.VIBRATE" />
记得在模拟器上是模拟不出震动的哦~~拷到手机里震一下吧~~
转载http://blog.csdn.net/ojlovecd/article/details/6362709
相关文章推荐
- MonoDroid学习笔记(十二)—— 您有一条新短信息,请注意查收。状态栏提醒
- MonoDroid学习笔记(十二)—— 您有一条新短信息,请注意查收。状态栏提醒
- iOS在状态栏上显示提醒信息的功能定制
- 恭喜你,微信支付申请资料已通过审核,相关信息已发送至你填写的邮箱,请注意查收
- Notification与状态栏信息
- 办公助手-用于办公系统的信息提醒
- angularsjs单独删除一条信息,不刷新页面
- 如何用asp.net向其他服务器post一条信息
- bootstrap datetimepicker 日期插件在火狐下出现一条报错信息的原因分析及解决办法
- PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
- 82. Notes客户端Print相同信息到状态栏的问题
- PHP实现每隔1秒(一秒)输出一条信息:禁用PHP缓存和web服务器(兼容Nginx、Apache)缓存、浏览器缓存
- 新闻更新时通过微信提醒(以SJTU奖学金信息为例)
- mysql连接查询-主从表连接查询:获取从表最新一条表记录;获取没有从表记录的主表信息
- Android状态栏提醒(Notification,NotificationManager)的使用!
- 用python+selenium从百度获取本地明日的天气信息并根据温度情况设置提醒
- 用什么方法可以在业务批量操作的时候保持原子性?例如删除多条文章,但是在中 间有一条被删除了,假设出现了错误,如何让整个操作回滚,并定位错误信息?
- 一条SQL搞定信息增益的计算
- Dynamics Ax状态栏自定义信息
- Redis系列~服务器信息(十二)