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();
        }

  • 相关阅读:
    [转]Delphi DLL的创建、静态 以及动态调用
    Delphi txt文件的操作(读取、写入)
    为什么要使用动态链接库(DLL)
    TStringGrid 实现下拉框
    Ryzen 4000'Vermeer' CPU和Radeon RX'Big Navi'图形卡
    AMD Ryzen 5000‘Cezanne’APU
    AMD–7nm “Rome”芯片SOC体系结构,支持64核
    ARM Cortex-M嵌入式C基础编程(下)
    ARM Cortex-M嵌入式C基础编程(上)
    基于ARM Cortex-M的SoC存储体系结构和实战
  • 原文地址:https://www.cnblogs.com/superWolf/p/4646938.html
Copyright © 2011-2022 走看看