zoukankan      html  css  js  c++  java
  • android显示通知栏Notification以及自定义Notification的View

    遇到的最大的问题是监听不到用户清除通知栏的广播。所以是不能监听到的。

    自定义通知栏的View,然后service运行时更改notification的信息。

    /**
         * Show a notification while this service is running.
         * 在service运行时,显示通知信息
         */
        public void showNotification() {
         NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    CharSequence text = getText(R.string.app_name); Notification notification = new Notification(R.drawable.img_step_counter_user, null, System.currentTimeMillis());       // 标志位的设置:应设置为可以自动取消,这样用户就可以取消他,如果设置为Intent.FLAG_ACTIVITY_CLEAR_TOP | Notification.FLAG_ONGOING_EVENT;则会一直显示通知
    // notification.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP | Notification.FLAG_ONGOING_EVENT; notification.flags = Notification.FLAG_AUTO_CANCEL; Intent pedometerIntent = new Intent(); pedometerIntent.setComponent(new ComponentName(this, StepCounterHomePageActivity.class)); pedometerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, pedometerIntent, 0); notification.setLatestEventInfo(this, text, getText(R.string.notification_subtitle)+": "+SensorData.stepNum+" 步", contentIntent);     // 其中R.layout.notification是一个布局文件
    notification.contentView = new RemoteViews(getPackageName(),R.layout.notification); notification.contentView.setViewVisibility(R.id.usermessage, View.VISIBLE); notification.contentView.setViewVisibility(R.id.notification_background, View.GONE); notification.contentView.setViewVisibility(R.id.notification_layout, View.GONE); notification.contentView.setTextViewText(R.id.tv_user_name_step, "掌上医生"); notification.contentView.setTextViewText(R.id.tv_use_date_step, "计算步数: "+SensorData.stepNum+"步"); mNM.notify(R.string.app_name, notification); }

      

  • 相关阅读:
    Springboot + Atomikos + Druid + Mysql 实现JTA分布式事务
    JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数
    java.net.UnknownHostException: lc001 未知的网络服务
    Maven 多模块引用版本的问题 java.lang.NoSuchMethodError
    Maven项目运行Junit测试用例 出现ClassNotFound
    CAS5.X 集群配置 初版
    调试CAS源码步骤
    openhtmltopdf 支持自定义字体、粗体
    Java HTML to PDF 支持SVG
    .Net 框架
  • 原文地址:https://www.cnblogs.com/Jackie-zhang/p/5220435.html
Copyright © 2011-2022 走看看