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

自定义通知栏的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); }

  

posted on 2016-02-26 14:53  灯火、阑珊处  阅读(6385)  评论(0编辑  收藏  举报