import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.content.Context;import android.content.ContextWrapper;import android.graphics.Color;public class NotificationHelper extends ContextWrapper {    private NotificationManager manager;    private Notification.Builder mBuilder;    public static final String PRIMARY_CHANNEL_ID = "default";    public static final String SECONDARY_CHANNEL_ID = "second";    public NotificationHelper(Context ctx) {        super(ctx);        NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID,                getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);        chan1.setLightColor(Color.GREEN);        chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);        getManager().createNotificationChannel(chan1);        NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL_ID,                getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);        chan2.setLightColor(Color.BLUE);        chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);        getManager().createNotificationChannel(chan2);    }    public Notification.Builder getNotificationPublic(String title, String body) {        return this.mBuilder = new Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL_ID)                 .setContentTitle(title)                 .setContentText(body)                 .setSmallIcon(getSmallIcon())                 .setAutoCancel(true);    }    public Notification.Builder getNotificationPrivate(String title, String body) {        return this.mBuilder = new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL_ID)                .setContentTitle(title)                .setContentText(body)                .setSmallIcon(getSmallIcon())                .setAutoCancel(true);    }    public void notify(int id, Notification.Builder notification) {        getManager().notify(id, notification.build());    }    public void notify(int id) {        getManager().notify(id, this.mBuilder.build());    }    private int getSmallIcon() {        return android.R.drawable.stat_sys_download;    }    private NotificationManager getManager() {        if (manager == null) {            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        }        return manager;    }}

使用示例:

        final NotificationHelper notificationHelper = new NotificationHelper(this);        final Notification.Builder mBuilder = notificationHelper.getNotificationPrivate("Picture Download", "Download in progress");        mBuilder.setOnlyAlertOnce(true);        new Thread(                new Runnable() {                    @Override                    public void run() {                        int incr;                        for (incr = 0; incr <= 100; incr+=5) {                            mBuilder.setProgress(100, incr, false);                            notificationHelper.notify(1);                            try {                                Thread.sleep(1*1000);                            } catch (InterruptedException e) {                                Log.d(TAG, "sleep failure");                            }                        }                        incr-=5;                        mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);                        mBuilder.setContentText(incr+"%")                                .setProgress(0,0,false);                        notificationHelper.notify(1);                    }                }        ).start();

更多相关文章

  1. android SQLite数据库使用实例
  2. Android(安卓)- LayoutInflater 的使用
  3. Android中TextView所带的各类属性的使用
  4. Android高级工程师每日一面试题精选!(1——15题)持续更新!
  5. Android(安卓)原生WebView的使用
  6. Android(安卓)原生WebView的使用
  7. 《Android高级进阶》— Android(安卓)书籍
  8. 《第一行代码Android》笔记
  9. 一、mono for android学习:什么是mono for android(使用C#开发andr

随机推荐

  1. Android开发之style属性和预定义样式
  2. Android热更新三:Android类加载机制
  3. Android应用开发——系统自带样式Android
  4. android 中系统自带的主题与样式(theme a
  5. Android(安卓)标签的主题样式
  6. android综合资讯App、自定义悬浮框、屏幕
  7. android 中系统自带的主题与样式(theme a
  8. Android(安卓)imageView图片按比例缩放
  9. 文章标题
  10. Android(安卓)app version code and name