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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM