Android 官方通知工具类Notification

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

你可能感兴趣的:(android)