当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发入门之Notification用法分析

Android开发入门之Notification用法分析

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

本文实例讲述了android中notification用法。分享给大家供大家参考,具体如下:

notification可以理解为通知的意思一般用来显示广播信息 用notification就必须要用到notificationmanager

想用notification一般有三个步骤,如下所示

① 一般获得系统级的服务notificationmanager。

调用context.getsystemservice(notification_service)方法即可返回notificationmanager实例

② 实例化notification,并设置其属性

用notification构造函数 public notification(int icon, charsequence tickertext, long when)构造notification实例

③ 通过notificationmanager发通知就ok了

notificationmanager有两个方法:notify()发出通知  cancel(...)取消通知

下面通过一个代码实例来介绍一下notification

先初始化notificationmanager和notification两个成员变量

notificationmanager = (notificationmanager)this.getsystemservice(notification_service);
notification = new notification(r.drawable.touxiang,"信息",system.currenttimemillis());

ps:notification构造函数里传的参数就是这样显示的第一个参数是 图像,第二个是标题 :信息 ,第三个是系统时间 (见图一) 之所以在这说一下这个构造函数是想与下面的setlatesteventinfo(...)作个区别。

(  图一   )

(   图二    )

布局就不贴了 是两个button  看看发送按钮

sendbutton.setonclicklistener(new onclicklistener()
{
  @override
  public void onclick(view v)
  {
    intent intent = new intent(notificationactivity.this,notificationactivity.class);
    pendingintent pendingintent = pendingintent.getactivity(notificationactivity.this, 0, intent, 0);
    notification.setlatesteventinfo(notificationactivity.this, "你的一条信息", "来自张三的信息", pendingintent);
    notificationmanager.notify(id,notification);
    notificationmanager.notify(id+1, notification);
  }
})

setlatesteventinfo(...)里面所传的参数的效果图:(见图二)通知里面有两条完全一样的消息,是的 你没看错,这是因为我用notificationmanager  notify(通知)了两次 而且id不同,id是int型是通知信息的标示符。虽然我上面两条信息是一模一样的,但由于id的不同 , 所以android还是会显示两条信息。

在此说一下参数pendingintent的在setlatesteventinfo里所扮演的角色,是啥子意思呢?pendingintent可以在另外的地方执行,不是立即意图。当用户点击扩展通知的时候 pendingintent意图才开始执行,例如图二 我点击其中一个消息后,立马就进入另外一个activity...

正如上面看到的那样,除了为notification设置图标,标题外还可以设置提示音,震动,闪光灯 详情请见我转的一片文章notification使用详解.....

package com.study.android;
import android.app.activity;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.content.context;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class mainactivity extends activity {
  private button startbtn;
  private button cancelbtn;
  private static final int hello_id = 1;
  notificationmanager mnotificationmanager;
  notification mnotification;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    startbtn = (button)findviewbyid(r.id.startbtn);
    cancelbtn = (button)findviewbyid(r.id.cancelbtn);
    // ① 获取notificationmanager的引用
    string ns = context.notification_service;
    mnotificationmanager = (notificationmanager)this.getsystemservice(ns);
    // ② 初始化notification
    int icon = r.drawable.ic_launcher;
    charsequence tickertext = "hello";
    long when = system.currenttimemillis();
    mnotification = new notification(icon,tickertext,when);
    mnotification.defaults = notification.default_all;
    mnotification.flags |= notification.flag_no_clear;
    mnotification.flags |= notification.flag_show_lights;
    // ③ 定义notification的消息 和 pendingintent
    context context = this;
    charsequence contenttitle ="my notification";
    charsequence contenttext = "hello world";
    intent notificationintent = new intent(this,mainactivity.class);
    pendingintent contentintent = pendingintent.getactivity(context, 0, notificationintent,0 );
    mnotification.setlatesteventinfo(context, contenttitle, contenttext, contentintent);
    // ④ 把封装好的notification传入notificationmanager
    // 开启通知
    startbtn.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        mnotificationmanager.notify(hello_id,mnotification);
      }
    });
    // 取消通知
    cancelbtn.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        mnotificationmanager.cancel(hello_id);
      }
    });
  }
}

代码中有创建notification步骤,详情可以看一下。

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android编程之activity操作技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网