zoukankan      html  css  js  c++  java
  • 在程序中点击home键后将程序通知显示到状态栏中

    当程序处于后台运作的时候,Activity处于onStop状态,so只要在onStop方法中将程序运行状态显示在状态栏即可

        //在状态栏显示程序通知
        private void showNotification() {
            // 创建一个NotificationManager的引用
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(android.content.Context.NOTIFICATION_SERVICE);

            // 定义Notification的各种属性
            Notification notification = new Notification(R.drawable.ic_launcher,
                    "superGao", System.currentTimeMillis());
            notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
            notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
            notification.defaults = Notification.DEFAULT_LIGHTS;
            notification.ledARGB = Color.BLUE;
            notification.ledOnMS = 5000;

            // 设置通知的事件消息
            CharSequence contentTitle = "superGao"; // 通知栏标题
            CharSequence contentText = "love"; // 通知栏内容
            Intent notificationIntent = new Intent(this, FirstActivity.class); // 点击该通知后要跳转的Activity
            PendingIntent contentItent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            notification.setLatestEventInfo(this, contentTitle, contentText,
                    contentItent);

            // 把Notification传递给NotificationManager
            notificationManager.notify(0, notification);

        }

        /**
         * 当此Activity处于后台工作时, 在状态栏显示通知
         */
        @Override
        protected void onStop() {
            showNotification();
            super.onStop();
        }

    当程序再次进入运行界面时,Activity处于onResume状态,so只要在onResume方法中去掉状态栏的程序运行信息即可

        /**
         * 此Activity启动后关闭状态栏的通知
         */
        @Override
        protected void onResume() {
            // 启动后删除之前我们定义的通知
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(0);
            super.onResume();
        }

  • 相关阅读:
    Oracle备份恢复之无备份情况下恢复undo表空间
    Oracle HA 之 测试RAC的功能
    (转)Python——functools
    (转)Python标准库:内置函数print(*objects, sep=' ', end=' ', file=sys.stdout, flush=False)
    (转)用Python写堡垒机项目
    (转)Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)
    (转)Linux中的位图
    (转)Python标准库02 时间与日期 (time, datetime包)
    (转)Python 3 collections.defaultdict() 与 dict的使用和区别
    (转)python高级:列表解析和生成表达式
  • 原文地址:https://www.cnblogs.com/superWolf/p/4646938.html
Copyright © 2011-2022 走看看