直播平臺原始碼,通知欄中顯示滑動的進度條

zhibo系統開發發表於2022-03-17

直播平臺原始碼,通知欄中顯示滑動的進度條實現的相關程式碼

private NotificationManager notificationManager;
private NotificationCompat.Builder builder;
private NotificationClickReceiver notificationClickReceiver;
 
public class DownloadManager {
private static final String TAG = "DownloadManager";
private Context mContext;
private String downloadUrl;
private String savePath;
private DownloadRecord downloadRecord;
    /**
     * 下載
     *
     * @param context 上下文
     * @param downloadUrl 下載地址
     * @param savePath 下載後儲存到本地的路徑
     */
    public void download(Context context, String downloadUrl, String savePath) {
        this.mContext = context;
        this.downloadUrl = downloadUrl;
        this.savePath = savePath;
        try {
            downloadRecord = Downloader.createRecord(downloadUrl, savePath,
                    new DownloadListenerAdapter() {
                        @Override
                        public void onTaskStart(DownloadRecord record) {
                            Log.d(TAG, "onTaskStart");
                            initNotification();
                        }
 
                        public void onTaskPause(DownloadRecord record) {
                            Log.d(TAG, "onTaskPause");
                        }
 
                        public void onTaskCancel(DownloadRecord record) {
                            Log.d(TAG, "onTaskCancel");
                        }
 
                        public void onProgressChanged(DownloadRecord record, int progress) {
                            Log.d(TAG, "onProgressChanged progress=" + progress);
                            updateNotification(progress);
                        }
 
                        @Override
                        public void onTaskSuccess(DownloadRecord record) {
                            Log.d(TAG, "onTaskSuccess");
                            showInstall();
                        }
 
                        @Override
                        public void onTaskFailure(DownloadRecord record, Throwable throwable) {
                            Log.e(TAG, "onTaskFailure error=" + throwable);
                            updateNotification(-1);
                        }
                    });
        } catch (Exception x) {
            Log.e(TAG, "download error=" + x);
        }
    }
 
    /**
     * 初始化通知
     */
    private void initNotification() {
        try {
            notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            // Android8.0及以後的方式
            if (Build.VERSION.SDK_INT >= 26) {
                // 建立通知渠道
                NotificationChannel notificationChannel = new NotificationChannel("download_channel", "下載",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.enableLights(false); //關閉閃光燈
                notificationChannel.enableVibration(false); //關閉震動
                notificationChannel.setSound(null, null); //設定靜音
                notificationManager.createNotificationChannel(notificationChannel);
            }
            builder = new NotificationCompat.Builder(mContext, "download_channel");
            builder.setContentTitle("已下載(0%)") //設定標題
                    .setSmallIcon(mContext.getApplicationInfo().icon) //設定小圖示
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
                            mContext.getApplicationInfo().icon)) //設定大圖示
                    .setPriority(NotificationCompat.PRIORITY_MAX) //設定通知的優先順序
                    .setAutoCancel(false) //設定通知被點選一次不自動取消
                    .setSound(null) //設定靜音
                    .setContentText("正在下載 點選取消") //設定內容
                    .setProgress(100, 0, false) //設定進度條
                    .setContentIntent(createIntent()); //設定點選事件
            notificationManager.notify(100, builder.build());
        } catch (Exception x) {
            Log.e(TAG, "initNotification error=" + x);
        }
    }
 
    /**
     * 重新整理通知
     *
     * @param progress 百分比,此值小於0時不重新整理進度條
     */
    private void updateNotification(int progress) {
        if (builder == null) {
            return;
        }
        if (progress >= 0) {
            builder.setContentTitle("已下載(" + progress + "%)");
            builder.setProgress(100, progress, false);
        }
        if (downloadRecord == null || downloadRecord.getState() == DownloadRecord.STATE_FAILURE) {
            builder.setContentText("下載失敗 點選重試");
        } else if (progress == 100) {
            builder.setContentText("下載完成 點選安裝");
            builder.setAutoCancel(true);
        }
        notificationManager.notify(100, builder.build());
    }
 
    /**
     * 設定通知點選事件
     *
     * @return 點選事件
     */
    private PendingIntent createIntent() {
        Intent intent = new Intent(mContext.getPackageName() + ".upgrade.notification");
        intent.setPackage(mContext.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }
 
    /**
     * 註冊通知點選監聽
     */
    private void registerReceiver() {
        notificationClickReceiver = new NotificationClickReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(mContext.getPackageName() + ".upgrade.notification");
        mContext.registerReceiver(notificationClickReceiver, intentFilter);
    }
 
    /**
     * 處理通知欄點選事件
     */
    public class NotificationClickReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!(mContext.getPackageName() + ".upgrade.notification").equals(action)) {
                return;
            }
            if (downloadRecord != null) {
                int state = downloadRecord.getState();
                Log.d(TAG, "onReceive state=" + state);
                switch (state) {
                    case DownloadRecord.STATE_CREATE:
                    case DownloadRecord.STATE_PENDING:
                    case DownloadRecord.STATE_RUNNING:
                    case DownloadRecord.STATE_PAUSE:
                        // 關閉通知欄
                        notificationManager.cancel(100);
                        break;
                    case DownloadRecord.STATE_SUCCESS:
                        // 顯示安裝確認彈窗
                        showInstallAlert(true);
                        break;
                    case DownloadRecord.STATE_FAILURE:
                        // 重新下載
                        download(mContext, this.downloadUrl, this.savePath);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}


以上就是直播平臺原始碼,通知欄中顯示滑動的進度條實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2871673/,如需轉載,請註明出處,否則將追究法律責任。

相關文章