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

      

  • 相关阅读:
    【Vue】源码——编译过程
    FreeRADIUS 、DaloRADIUS 搭建记录
    docker 学习笔记
    wget 技巧
    Centos 部署Cobbler系统
    Cloudstack 安装记录
    利用Google GCM发送push通知到Android客户端
    利用ApnsPHP包向IOS推送消息
    Linux下SCP的使用
    Android Google购买PHP服务器端验证(订阅购买和一次性购买)
  • 原文地址:https://www.cnblogs.com/Jackie-zhang/p/5220435.html
Copyright © 2011-2022 走看看