当前位置: 移动技术网 > IT编程>移动开发>Android > Android中关于Notification及NotificationManger的详解

Android中关于Notification及NotificationManger的详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

极品飞车9解压密码,得失不萦于怀,龙年快乐电影最新地址2016

android状态栏提醒

在android中提醒功能也可以用alertdialog,但是我们要慎重的使用,因为当使用alertdialog的时候,用户正在进行的操作将会被打断,因为当前焦点被alertdialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用alertdialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

步骤:

一、添加布局对象

复制代码 代码如下:

<button
android:id="@+id/showbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shownotification" />

<button
android:id="@+id/cancelbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cancelnotification" />


二、修改mianactivity继承处activity并实现接口onclicklistener
复制代码 代码如下:

public class mainactivity extends activity implements onclicklistener {
 private context mcontext = this;
 private button showbtn, calclebtn;
 private notification noti;
 private notificationmanager notimanger;
 private static int notification_id = 0x0001;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  setupviews();
 }

 private void setupviews() {
  showbtn = (button) findviewbyid(r.id.showbutton);
  calclebtn = (button) findviewbyid(r.id.cancelbutton);
  noti = new notification(r.drawable.ic_launcher, "this is a notification", system.currenttimemillis());
  noti.defaults = notification.default_sound;// 使用默认的提示声音
  noti.defaults |= notification.default_vibrate;// 添加震动
  notimanger = (notificationmanager) this.getsystemservice(mcontext.notification_service);//获取nofificationmanger对象
  showbtn.setonclicklistener(this);//让activity实现接口onclicklistener可以简单的通过此两行代码添加按钮点击响应事件
  calclebtn.setonclicklistener(this);
 }

 // 按钮点击事件响应
 @override
 public void onclick(view v) {
  if (v == showbtn) {
   intent intent = new intent(this.getapplicationcontext(),this.getclass());
   // 设置intent.flag_activity_new_task
   intent.setflags(intent.flag_activity_new_task);
   pendingintent contentintent = pendingintent.getactivity(this, 0, intent, 0);
   // noti.setlatesteventinfo(context, contenttitle, contenttext, contentintent)设置(上下文,标题,内容,pendinginteng)
   noti.setlatesteventinfo(this, "10086", "你从此以后免除所有话费", contentintent);
   // 发送通知(消息id,通知对象)
   notimanger.notify(notification_id, noti);
  } else if (v == calclebtn) {
   // 取消通知(id)
   notimanger.cancel(notification_id);
  }
 }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网