Java tutorial
/* * Copyright 2017 huxizhijian * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.huxizhijian.hhcomicviewer.utils; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v4.app.NotificationCompat; import org.huxizhijian.hhcomicviewer.R; import org.huxizhijian.hhcomicviewer.model.ComicChapter; import org.huxizhijian.hhcomicviewer.service.DownloadManagerService; import org.huxizhijian.hhcomicviewer.ui.main.MainActivity; import org.huxizhijian.hhcomicviewer.ui.download.DownloadingChapterActivity; import org.huxizhijian.hhcomicviewer.ui.download.OfflineComicDownloadActivity; /** * * Created by wei on 2016/9/14. */ public class NotificationUtil { private static NotificationManager sNotificationManager; private static NotificationUtil sNotificationUtil; private Context mContext; private Bitmap mBitmap = null; private NotificationUtil(Context context) { //? sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //? this.mContext = context; this.mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher); } public static NotificationUtil getInstance(Context context) { if (sNotificationUtil == null) { sNotificationUtil = new NotificationUtil(context); } return sNotificationUtil; } /** * * * @param comicChapter */ public void showNotification(DownloadManagerService service, ComicChapter comicChapter) { NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); // builder.setSmallIcon(android.R.drawable.stat_sys_download).setLargeIcon(mBitmap).setAutoCancel(false) .setOngoing(true).setCategory(NotificationCompat.CATEGORY_PROGRESS); builder.setTicker(comicChapter.getChapterName() + " "); //? CharSequence contentTitle = comicChapter.getChapterName() + " - "; // ? CharSequence contentText = 0 + "/" + comicChapter.getPageCount(); // ? //? Intent intent0 = new Intent(mContext, MainActivity.class); //??? Intent intent1 = new Intent(mContext, DownloadingChapterActivity.class); //? Intent[] intents = { intent0, intent1 }; //?PendingIntent PendingIntent contentIntent = PendingIntent.getActivities(mContext, 0, intents, 0); // builder.setContentTitle(contentTitle).setContentText(contentText).setContentIntent(contentIntent); builder.setProgress(comicChapter.getPageCount(), 0, true); //false?true? Notification notification = builder.build(); //?service?? service.startForeground(comicChapter.getId(), notification); } public void cancelNotification(DownloadManagerService service, int id) { //? // sNotificationManager.cancel(id); //service??? service.stopForeground(true); } /** * * * @param comicChapter */ public void finishedNotification(ComicChapter comicChapter) { NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); //?? builder.setSmallIcon(android.R.drawable.stat_sys_download_done).setLargeIcon(mBitmap) .setTicker(comicChapter.getChapterName() + " ").setOngoing(false).setAutoCancel(true); //? CharSequence contentTitle = comicChapter.getComicTitle(); // ? CharSequence contentText = comicChapter.getChapterName() + " - "; // ? //? Intent intent0 = new Intent(mContext, MainActivity.class); //??? Intent intent1 = new Intent(mContext, OfflineComicDownloadActivity.class); //? Intent[] intents = { intent0, intent1 }; //?PendingIntent PendingIntent contentIntent = PendingIntent.getActivities(mContext, 0, intents, 0); // builder.setContentTitle(contentTitle).setContentText(contentText).setContentIntent(contentIntent); //?? builder.setAutoCancel(true); Notification notification = builder.build(); //? sNotificationManager.notify(Constants.FINISHED_NOTIFICATION_ID, notification); } /** * ? */ public void updateNotification(int id, ComicChapter comicChapter) { /*//?? int progress = (int) ((float) downloadPosition / (float) pageCount * 100f); notification.contentView.setProgressBar(R.id.progress_bar_notification, 100, progress, false); // notification.contentView.setTextViewText(R.id.textView_notification_progress, downloadPosition + "/" + pageCount);*/ NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); // builder.setSmallIcon(android.R.drawable.stat_sys_download).setLargeIcon(mBitmap).setAutoCancel(false) .setOngoing(true).setCategory(NotificationCompat.CATEGORY_PROGRESS); builder.setTicker(comicChapter.getChapterName() + " "); //? CharSequence contentTitle = comicChapter.getChapterName() + " - "; // ? CharSequence contentText = comicChapter.getDownloadPosition() + "/" + comicChapter.getPageCount(); // ? //? Intent intent0 = new Intent(mContext, MainActivity.class); //??? Intent intent1 = new Intent(mContext, DownloadingChapterActivity.class); //? Intent[] intents = { intent0, intent1 }; //?PendingIntent PendingIntent contentIntent = PendingIntent.getActivities(mContext, 0, intents, 0); // builder.setContentTitle(contentTitle).setContentText(contentText).setContentIntent(contentIntent); builder.setProgress(comicChapter.getPageCount(), comicChapter.getDownloadPosition(), false); //false? Notification notification = builder.build(); // sNotificationManager.notify(id, notification); } }