当前位置: 首页 > news >正文

[Android Pro] Notification的使用

Android 4.0以前: 

1: 普通的notification

private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    private void showNotification(Store store) {
        Notification notification = new Notification();
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_ALL;
        notification.icon = R.drawable.ic_launch;
        notification.when = System.currentTimeMillis();

        Intent intent = new Intent(this,AlarmActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        intent.putExtra("store", store);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT

         //Change the name of the notification here
        notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);
        notificationManager.notify(NOTIFY_ID, notification);
    }

2: 将服务service设置为前台notification

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        setServiceForground();
    }

    @SuppressWarnings("deprecation")
    public void setServiceForground() {
        Notification notification = new Notification(R.drawable.ic_launcher,
        "my_service_name", System.currentTimeMillis());
    //要添加newtask PendingIntent p_intent
= PendingIntent.getActivity(this, 0, new Intent( this, MainActivity.class), 0); notification.setLatestEventInfo(this, "MyServiceNotification", "MyServiceNotification is Running!", p_intent); startForeground(0x1982, notification); }
@Override
public IBinder onBind(Intent intent) { return null; } }

 

Android 4.0 以后: 

将服务service设置为前台notification 

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        setServiceForground();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public void setServiceForground() {
        Notification.Builder build = new Notification.Builder(this);
//      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(
//            this, MainActivity.class), 0);
//      build.setContentIntent(contentIntent);
        build.setSmallIcon(R.drawable.ic_launcher);    
        build.setContentTitle("MyServiceNotification");
        build.setContentText("MyServiceNotification is Running!");
        startForeground(0x1982, build.build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

 

相关文章:

  • Lamp架构扩展
  • Ext2 的方法
  • Service 详解
  • Google开发新的Aspeed控制驱动程序
  • FFMPEG4.0 音频解码解封装
  • python 基础使用list、dict、set、可变与不可变对象
  • Nginx 400错误研究
  • JavaScript函数使用知识点回顾
  • Unity移动端实时阴影绘制
  • 6.2docker(二)
  • jsp直接返回验证码
  • iis7负载均衡
  • 目录服务不能与此服务器复制,因为距上一次与此服务器复制的时间已经超过了 tombstone 生存时间。...
  • linux命令 route
  • 基于 HTML5 WebGL 的 3D 场景中的灯光效果
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 2017-09-12 前端日报
  • dva中组件的懒加载
  • exif信息对照
  • Flex布局到底解决了什么问题
  • gops —— Go 程序诊断分析工具
  • Java|序列化异常StreamCorruptedException的解决方法
  • JS数组方法汇总
  • MaxCompute访问TableStore(OTS) 数据
  • nginx 负载服务器优化
  • React-redux的原理以及使用
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • Vue ES6 Jade Scss Webpack Gulp
  • Vultr 教程目录
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 笨办法学C 练习34:动态数组
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 高程读书笔记 第六章 面向对象程序设计
  • 汉诺塔算法
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 新书推荐|Windows黑客编程技术详解
  • 原生Ajax
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • #《AI中文版》V3 第 1 章 概述
  • #1014 : Trie树
  • #微信小程序:微信小程序常见的配置传值
  • $refs 、$nextTic、动态组件、name的使用
  • (3)nginx 配置(nginx.conf)
  • (4) PIVOT 和 UPIVOT 的使用
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • ***监测系统的构建(chkrootkit )
  • .bashrc在哪里,alias妙用
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET Standard 的管理策略
  • .NET 自定义中间件 判断是否存在 AllowAnonymousAttribute 特性 来判断是否需要身份验证
  • .NET/C# 的字符串暂存池
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d