android 8.0 帶進(jìn)度條通知欄

最近工作比較繁忙,一直沒(méi)抽空更新博客,
簡(jiǎn)單封裝了下進(jìn)度條工具類(lèi),有需要的直接拿去用,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();//可否繞過(guò),請(qǐng)勿打擾模式
            channel.enableLights(true);//閃光
            channel.setLockscreenVisibility(VISIBILITY_SECRET);//鎖屏顯示通知
            channel.setLightColor(Color.RED);//指定閃光是的燈光顏色
            channel.canShowBadge();//桌面laucher消息角標(biāo)
            channel.enableVibration(true);//是否允許震動(dòng)
            channel.setSound(null, null);
            //channel.getAudioAttributes();//獲取系統(tǒng)通知響鈴聲音配置
            channel.getGroup();//獲取通知渠道組
            channel.setBypassDnd(true);//設(shè)置可以繞過(guò),請(qǐng)勿打擾模式
            channel.setVibrationPattern(new long[]{100, 100, 200});//震動(dòng)的模式,震3次,第一次100,第二次100,第三次200毫秒
            channel.shouldShowLights();//是否會(huì)閃光
            //通知管理者創(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;
    }
}

有時(shí)間寫(xiě)篇詳細(xì)的博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評(píng)論 25 709
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,976評(píng)論 2 59
  • 好久沒(méi)寫(xiě)日記,最近幾個(gè)月整天都是混混惡惡,都不知道在干什么。把時(shí)間花在無(wú)聊的事上,我都快不認(rèn)識(shí)我自己了,所以我要...
    qbSally閱讀 135評(píng)論 0 0
  • (七十五)正確的打開(kāi)方式 (七十六)論愛(ài)情的盲目性 室韋的阿布王子,此次攜使團(tuán)來(lái)大唐給皇太后祝壽,最真實(shí)的意圖,是...
    筆間流年閱讀 286評(píng)論 2 3
  • 媽媽?zhuān)瞧臎龅臑┩?來(lái)不及記住您留下的蹣跚步履 一滴來(lái)自天上的的水 已將它灌溉成一片清澈的湖 陽(yáng)光下泛起絲絲漣漪...
    一葉微嵐閱讀 1,903評(píng)論 50 59

友情鏈接更多精彩內(nèi)容