当前位置: 移动技术网 > IT编程>移动开发>Android > android 通知Notification详解及实例代码

android 通知Notification详解及实例代码

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

江泽明的情人,韦小宝之娇妻成群,qq游戏你画我猜

android notification实例详解

1.使用builder模式来创建

2.必须要设置一个smallicon,还可以设置setticker

3.可以设置 setcontenttitle,setcontentinfo,setcontenttext,setwhen

4.可以设置setdefaults(闪屏,声音,震动),通过notification设置flags(能不能被清除)

5.发送需要获取一个notificationmanager(getsystemservice来获取);notify(int id,notification)

6.清除 manager.cancelall(清除所有) cancal(int id); 如果需要16一下的api也能访问,需要导入v4包,使用notificationcompat兼容类。

自定义通知

使用remoteviews来建立一个布局,然后使用setcontent()设置;

点击事件使用pendingintent来完成

下面是一个案例

mainactivity类

public class mainactivity extends activity {

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

  }

  public void clearnotification(view v) {
    // 通过代码来清除 no_clear
    // 清除也需要manager
    notificationmanager manager = (notificationmanager) getsystemservice(notification_service);
    // 只能清除由本应用程序发出去的通知
    manager.cancelall();
  }

  public void sendnotification(view v) {
    // 他是 在v4包中的通知,用于16以下版本发送通知
    // notificationcompat
    // 通知的创建
    notification.builder builder = new notification.builder(this);
    // notificationcompat.builder builder2 = new notificationcompat.builder(
    // this);
    // 显示在通知条上的图标 必须的
    builder.setsmallicon(r.drawable.ic_launcher);
    // 显示通知条上的文字 不是必须的
    builder.setticker("您有一条新的消息!!");

    // 状态栏中的
    builder.setcontenttitle("大标题");
    builder.setcontenttext("文本");
    builder.setwhen(system.currenttimemillis());
    builder.setcontentinfo("info");
    builder.setdefaults(notification.default_all);
    // 点击事件使用pending
    intent intent = new intent(this, mainactivity.class);
    pendingintent pi = pendingintent.getactivity(this, 1, intent,
        pendingintent.flag_update_current);
    builder.setcontentintent(pi);
    // 生成对象 16api以上,支持低版本需要使用v4包中的notificationcompat
    notification notify = builder.build();
    // 设置不能清除
    notify.flags = notification.flag_no_clear;

    // 如何将通知发送出去
    notificationmanager mananger = (notificationmanager) getsystemservice(notification_service);
    // 通知的唯一值,如果id重复,表明是更新一条通知,而不是新建
    mananger.notify((int) (math.random() * 1000), notify);
  }

  public void diynotification(view v) {
    // 展示在通知上面的视图
    remoteviews views = new remoteviews(getpackagename(), r.layout.layout);
    notification notification = new notification.builder(this)
        .setsmallicon(r.drawable.ic_launcher).setticker("自定义通知 ")
        // 布局
        .setcontent(views).build();
    // 使用remoteviews来设置点击事件
    views.settextcolor(r.id.tv, color.red);
    intent intent = new intent(this, oneactivity.class);
    // 音乐播放是放在service
    pendingintent pi = pendingintent.getactivity(this, 2, intent,
        pendingintent.flag_update_current);
    views.setonclickpendingintent(r.id.tv, pi);

    intent intent2 = new intent(this, mainactivity.class);

    pendingintent pi2 = pendingintent.getactivity(this, 1, intent2,
        pendingintent.flag_update_current);
    views.setonclickpendingintent(r.id.iv, pi2);
    // 发送
    notificationmanager notify = (notificationmanager) getsystemservice(notification_service);
    notify.notify(1, notification);

  }

}

oneactivity类

public class oneactivity extends activity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    textview tv = new textview(this);
    tv.settext("跳转界面");
    setcontentview(tv);
  }
}

activity.main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.lesson8_notification.mainactivity" >

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onclick="sendnotification"
    android:text="普通的通知" />

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onclick="clearnotification"
    android:text="清除所有通知" />

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onclick="diynotification"
    android:text="自定义通知" />

</linearlayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <textview
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="textview" />

  <imageview
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</linearlayout>

最后记得注册activity

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

相关文章:

验证码:
移动技术网