Android Notification的一個簡單應用(在Notification中嵌入一個進度條,并且這個Notification點擊消失但不會跳轉)

jopen 11年前發布 | 50K 次閱讀 Android Android開發 移動開發

  網上很多的例子都是直接獲取Notification對象來設置一個通知,其實Notification跟Dialog一樣,也有自己的Builder,可以用builder對象來設置一個Notification

    這個例子是在Notification中嵌入一個進度條,并且這個Notification點擊消失但不會跳轉(跟android的vcard文件導入時彈出的Notification一樣)

    NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(context);
        builder.setOngoing(true);
        builder.setProgress(total, current, false);//設置進度條,false表示是進度條,true表示是個走馬燈
        builder.setTicker(title);//設置title
        builder.setWhen(System.currentTimeMillis());
        builder.setContentTitle(content);//設置內容
        builder.setAutoCancel(true);//點擊消失
        builder.setSmallIcon(R.drawable.upload);
        builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0));//這句和點擊消失那句是“Notification點擊消失但不會跳轉”的必須條件,如果只有點擊消失那句,這個功能是不能實現的

        Notification noti = builder.getNotification();
        mNotificationManager.notify(id,noti);

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!