android notification
来源:互联网 发布:淘宝祛痘产品 编辑:程序博客网 时间:2024/05/18 03:02
今天抽空学习了一下android的状态栏如何使用。一般在手机有未接电话,收到消息或者挂着退出主界面的QQ,在状态栏会有一个Notification。
Figure 1. 通知消息的状态栏
传教通知栏
1)Get a reference to the NotificationManager
:获得系统级的服务NotificationManager
String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);2)Instantiate the
Notification
:实例化Notification,并设置其属性。int icon = R.drawable.notification_icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);3)Define the notification's message and
PendingIntent
:定义通知消息并PendingIntentContext context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);4)Pass the
Notification
to the NotificationManager
:private static final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification);如何改变 Notification 在“正在运行的”栏目下面的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv =
new
RemoteViews(Main.
this
.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text,
"Hello,there,I'm john."
);
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);
注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
//创建 NotificationManager,其中创建的sysNotifyMgr 对象负责“发出”与“取消” Notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,
//Notification notify = new Notification(R.drawable.icon, "Hello,there!", System.currentTimeMillis());
Notification notify = new Notification(); // 创建新通知
notify.icon = R.drawable.icon;
notify.tickerText = "Hello,there!";
notify.when = System.currentTimeMillis() + 200* 1000;
notify.number = 3; // 设置状态图标上显示的数字
//这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
//振动为Notification.DEFAULT_VIBRATE;
//Light为Notification.DEFAULT_LIGHTS
//全部为Notification.DEFAULT_ALL
//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
notify.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知
Intent intent = new Intent(LogoActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
notify.setLatestEventInfo(this,"test","hello Test",pi);// 更新提示内容
notificationManager.notify(1, notify);
- ANdroid Notification
- android notification
- android notification
- android notification
- Android Notification
- android notification
- android notification
- android Notification
- Android Notification
- android Notification
- Android notification
- android Notification
- android:Notification
- Android Notification
- Android Notification
- Android Notification
- Android notification
- android Notification
- GNU ARM汇编--(八)s3c2440的watchdog
- BMP文件格式详解
- Android的EidtText的一些研究,持续更新中!!!!!
- android源码解析 ---- camera 照相机 摄像机
- 多线程写图像文件的一点小测试(Boost + Gual)
- android notification
- Dialog背景透明无边框
- Component MSCOMCTL.OCX or one of its dependencies not currently registered
- 使用vc++建立一个utility工程
- multipart form-data boundary 说明
- 黑马程序员--java学习之享元与枚举
- 前端创作的总结
- DES加密解密算法(转)
- Mysql中表结构以及数据的分布