Android(安卓)官方通知工具类Notification
16lz
2021-12-04
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 SQLite数据库使用实例
- Android(安卓)- LayoutInflater 的使用
- Android中TextView所带的各类属性的使用
- Android高级工程师每日一面试题精选!(1——15题)持续更新!
- Android(安卓)原生WebView的使用
- Android(安卓)原生WebView的使用
- 《Android高级进阶》— Android(安卓)书籍
- 《第一行代码Android》笔记
- 一、mono for android学习:什么是mono for android(使用C#开发andr