当前位置: 首页 > 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 场景中的灯光效果
  • [数据结构]链表的实现在PHP中
  • 【面试系列】之二:关于js原型
  • ES6 学习笔记(一)let,const和解构赋值
  • EventListener原理
  • Github访问慢解决办法
  • HTTP中GET与POST的区别 99%的错误认识
  • Javascript弹出层-初探
  • mockjs让前端开发独立于后端
  • SpringCloud集成分布式事务LCN (一)
  • Sublime text 3 3103 注册码
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 电商搜索引擎的架构设计和性能优化
  • 猴子数据域名防封接口降低小说被封的风险
  • 类orAPI - 收藏集 - 掘金
  • 聊聊directory traversal attack
  • 聊聊hikari连接池的leakDetectionThreshold
  • 如何编写一个可升级的智能合约
  • 山寨一个 Promise
  • 新手搭建网站的主要流程
  • 在Unity中实现一个简单的消息管理器
  • ​configparser --- 配置文件解析器​
  • ​iOS安全加固方法及实现
  • ​MySQL主从复制一致性检测
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #考研#计算机文化知识1(局域网及网络互联)
  • (+4)2.2UML建模图
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (分类)KNN算法- 参数调优
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (转)重识new
  • ******之网络***——物理***
  • ../depcomp: line 571: exec: g++: not found
  • .gitignore
  • .md即markdown文件的基本常用编写语法
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .ui文件相关
  • @Conditional注解详解