热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android通知Notification详解及实例代码

这篇文章主要介绍了android通知Notification详解及实例代码的相关资料,需要的朋友可以参考下

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



  

layout.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


  

  



最后记得注册activity

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


推荐阅读
author-avatar
mobiledu2502914617
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有