最近工作比較繁忙,一直沒抽空更新博客,
簡單封裝了下進(jìn)度條工具類,有需要的直接拿去用,targetSdkVersion 可以指定到26
具體要定制的建議去官網(wǎng)看看
public class NotificationUtils {
private static NotificationManager manager;
private static NotificationManager getManager() {
if (manager == null) {
manager = (NotificationManager) App.getInstance().getSystemService(NOTIFICATION_SERVICE);
}
return manager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static Notification.Builder getNotificationBuilder(String title, String content, String channelId) {
//大于8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//id隨便指定
NotificationChannel channel = new NotificationChannel(channelId, App.getInstance().getPackageName(), NotificationManager.IMPORTANCE_DEFAULT);
channel.canBypassDnd();//可否繞過,請勿打擾模式
channel.enableLights(true);//閃光
channel.setLockscreenVisibility(VISIBILITY_SECRET);//鎖屏顯示通知
channel.setLightColor(Color.RED);//指定閃光是的燈光顏色
channel.canShowBadge();//桌面laucher消息角標(biāo)
channel.enableVibration(true);//是否允許震動
channel.setSound(null, null);
//channel.getAudioAttributes();//獲取系統(tǒng)通知響鈴聲音配置
channel.getGroup();//獲取通知渠道組
channel.setBypassDnd(true);//設(shè)置可以繞過飞蹂,請勿打擾模式
channel.setVibrationPattern(new long[]{100, 100, 200});//震動的模式蕾额,震3次豁跑,第一次100贰镣,第二次100酒繁,第三次200毫秒
channel.shouldShowLights();//是否會閃光
//通知管理者創(chuàng)建的渠道
getManager().createNotificationChannel(channel);
}
return new Notification.Builder(App.getInstance()).setAutoCancel(true).setChannelId(channelId)
.setContentTitle(title)
.setContentText(content).setSmallIcon(R.mipmap.ic_launcher);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void showNotification(String title, String content, int manageId, String channelId, int progress, int maxProgress) {
final Notification.Builder builder = getNotificationBuilder(title,content,channelId);
/* Intent intent = new Intent(this, SecondeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);*/
builder.setOnlyAlertOnce(true);
builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
builder.setProgress(maxProgress, progress, false);
builder.setWhen(System.currentTimeMillis());
getManager().notify(manageId, builder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void cancleNotification(int manageId) {
getManager().cancel(manageId);
}
}
自行替換
public class App extends Application {
private static App mApplication = null;
public static App getInstance(){
return myApplication;
}
@Override
public void onCreate() {
super.onCreate();
mApplication=this;
}
}
有時間寫篇詳細(xì)的博客