Notification使用举例

来源:互联网 发布:韩国人听歌用什么软件 编辑:程序博客网 时间:2024/06/05 10:55
<ul style="margin-top: 0px; margin-bottom: 0px; font-family: 微软雅黑; font-size: 14px; line-height: 21px;"><li style="background-color: inherit;"><span style="background-color: inherit; color: rgb(0, 112, 192); font-family: 宋体; font-size: 12pt;">创建一个<span style="font-family:Calibri;background-color: inherit;">Notification</span><span style="font-family:宋体;background-color: inherit;">的步骤。</span></span></li></ul><p style="margin-top: 5px; margin-bottom: 5px; font-family: 微软雅黑; font-size: 14px; line-height: 21px;"><span style="background-color: inherit; color: rgb(0, 112, 192); font-family: 宋体; font-size: 12pt;">思路:①得到<span style="font-family:Calibri;background-color: inherit;">NotificationManager</span><span style="font-family:宋体;background-color: inherit;">的引用;②初始化一个</span><span style="font-family:Calibri;background-color: inherit;">Notification</span><span style="font-family:宋体;background-color: inherit;">对象;③设置</span><span style="font-family:Calibri;background-color: inherit;">Notification</span><span style="font-family:宋体;background-color: inherit;">的参数;④使用</span><span style="font-family:Calibri;background-color: inherit;">NotificationManager</span><span style="font-family:宋体;background-color: inherit;">的</span><span style="font-family:Calibri;background-color: inherit;">notify(int, Notification)</span><span style="font-family:宋体;background-color: inherit;">方法发出通知。</span></span></p>
<span style="font-size:18px;">public void initNotifi() {String ns = Context.NOTIFICATION_SERVICE;</span>
<span style="font-size:18px;"><span style="white-space:pre"></span><strong>//得到NotificationManager的引用</strong>mNotificationManager = (NotificationManager) getActivity().getSystemService(ns);<span style="white-space:pre"><strong></strong></span><strong>//初始化一个Notification对象,并设置参数,闪灯、声音、icon</strong>notification = new Notification();notification.defaults |= Notification.DEFAULT_SOUND;notification.defaults |= Notification.DEFAULT_LIGHTS;notification.icon = R.drawable.shop;notification.when = System.currentTimeMillis();context = getActivity().getApplicationContext();contentTitle = "简付";contentText = "买单成功,点击查看!";}</span>


<span style="font-size:18px;">public void onClick(View v) {switch (v.getId()) {case R.id.bt_maidanimdia:<strong>//<span style="color: rgb(0, 112, 192); font-family: 宋体; font-size: 16px; line-height: 21px; white-space: pre; background-color: inherit;">使用</span><span style="color: rgb(0, 112, 192); font-size: 16px; line-height: 21px; white-space: pre; font-family: Calibri; background-color: inherit;">NotificationManager</span><span style="color: rgb(0, 112, 192); font-family: 宋体; font-size: 16px; line-height: 21px; white-space: pre; background-color: inherit;">的</span><span style="color: rgb(0, 112, 192); font-size: 16px; line-height: 21px; white-space: pre; font-family: Calibri; background-color: inherit;">notify(int, Notification)</span><span style="color: rgb(0, 112, 192); font-family: 宋体; font-size: 16px; line-height: 21px; white-space: pre; background-color: inherit;">方法发出通知。</span></strong>Intent notificationIntent = new Intent(getActivity(),CardTxnDetailsActivity.class);contentIntent = PendingIntent.getActivity(getActivity(), 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);mNotificationManager.notify(MAIDAN_ID, <strong>notification</strong>);getActivity().finish();break;</span>


                                             
0 0
原创粉丝点击