直播平台源码,通知栏中显示滑动的进度条
直播平台源码,通知栏中显示滑动的进度条实现的相关代码
1 | private NotificationManager notificationManager;<br> private NotificationCompat.Builder builder;<br> private NotificationClickReceiver notificationClickReceiver;<br> <br> public class DownloadManager {<br> private static final String TAG = "DownloadManager" ;<br> private Context mContext;<br> private String downloadUrl;<br> private String savePath;<br> private DownloadRecord downloadRecord;<br> /**<br> * 下载<br> *<br> * @param context 上下文<br> * @param downloadUrl 下载地址<br> * @param savePath 下载后保存到本地的路径<br> */ <br> public void download(Context context, String downloadUrl, String savePath) {<br> this.mContext = context;<br> this.downloadUrl = downloadUrl;<br> this.savePath = savePath;<br> try {<br> downloadRecord = Downloader.createRecord(downloadUrl, savePath,<br> new DownloadListenerAdapter() {<br> @Override<br> public void onTaskStart(DownloadRecord record) {<br> Log.d(TAG, "onTaskStart" );<br> initNotification();<br> }<br> <br> public void onTaskPause(DownloadRecord record) {<br> Log.d(TAG, "onTaskPause" );<br> }<br> <br> public void onTaskCancel(DownloadRecord record) {<br> Log.d(TAG, "onTaskCancel" );<br> }<br> <br> public void onProgressChanged(DownloadRecord record, int progress) {<br> Log.d(TAG, "onProgressChanged progress=" + progress);<br> updateNotification(progress);<br> }<br> <br> @Override<br> public void onTaskSuccess(DownloadRecord record) {<br> Log.d(TAG, "onTaskSuccess" );<br> showInstall();<br> }<br> <br> @Override<br> public void onTaskFailure(DownloadRecord record, Throwable throwable) {<br> Log.e(TAG, "onTaskFailure error=" + throwable);<br> updateNotification(-1);<br> }<br> });<br> } catch (Exception x) {<br> Log.e(TAG, "download error=" + x);<br> }<br> }<br> <br> /**<br> * 初始化通知<br> */ <br> private void initNotification() {<br> try {<br> notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);<br> // Android8.0及以后的方式<br> if (Build.VERSION.SDK_INT >= 26) {<br> // 创建通知渠道<br> NotificationChannel notificationChannel = new NotificationChannel("download_channel", "下载",<br> NotificationManager.IMPORTANCE_DEFAULT);<br> notificationChannel.enableLights(false); //关闭闪光灯<br> notificationChannel.enableVibration(false); //关闭震动<br> notificationChannel.setSound(null, null); //设置静音<br> notificationManager.createNotificationChannel(notificationChannel);<br> }<br> builder = new NotificationCompat.Builder(mContext, "download_channel");<br> builder.setContentTitle("已下载(0%)") //设置标题<br> .setSmallIcon(mContext.getApplicationInfo().icon) //设置小图标<br> .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),<br> mContext.getApplicationInfo().icon)) //设置大图标<br> .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级<br> .setAutoCancel(false) //设置通知被点击一次不自动取消<br> .setSound(null) //设置静音<br> .setContentText("正在下载 点击取消") //设置内容<br> .setProgress(100, 0, false) //设置进度条<br> .setContentIntent(createIntent()); //设置点击事件<br> notificationManager.notify(100, builder.build());<br> } catch (Exception x) {<br> Log.e(TAG, "initNotification error=" + x);<br> }<br> }<br> <br> /**<br> * 刷新通知<br> *<br> * @param progress 百分比,此值小于0时不刷新进度条<br> */<br> private void updateNotification(int progress) {<br> if (builder == null) {<br> return;<br> }<br> if (progress >= 0) {<br> builder.setContentTitle("已下载(" + progress + "%)");<br> builder.setProgress(100, progress, false);<br> }<br> if (downloadRecord == null || downloadRecord.getState() == DownloadRecord.STATE_FAILURE) {<br> builder.setContentText("下载失败 点击重试");<br> } else if (progress == 100) {<br> builder.setContentText("下载完成 点击安装");<br> builder.setAutoCancel(true);<br> }<br> notificationManager.notify(100, builder.build());<br> }<br> <br> /**<br> * 设置通知点击事件<br> *<br> * @return 点击事件<br> */<br> private PendingIntent createIntent() {<br> Intent intent = new Intent(mContext.getPackageName() + ".upgrade.notification");<br> intent.setPackage(mContext.getPackageName());<br> intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br> PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent,<br> PendingIntent.FLAG_UPDATE_CURRENT);<br> return pendingIntent;<br> }<br> <br> /**<br> * 注册通知点击监听<br> */<br> private void registerReceiver() {<br> notificationClickReceiver = new NotificationClickReceiver();<br> IntentFilter intentFilter = new IntentFilter();<br> intentFilter.addAction(mContext.getPackageName() + ".upgrade.notification");<br> mContext.registerReceiver(notificationClickReceiver, intentFilter);<br> }<br> <br> /**<br> * 处理通知栏点击事件<br> */<br> public class NotificationClickReceiver extends BroadcastReceiver {<br> @Override<br> public void onReceive(Context context, Intent intent) {<br> String action = intent.getAction();<br> if (!(mContext.getPackageName() + ".upgrade.notification").equals(action)) {<br> return;<br> }<br> if (downloadRecord != null) {<br> int state = downloadRecord.getState();<br> Log.d(TAG, "onReceive state=" + state);<br> switch (state) {<br> case DownloadRecord.STATE_CREATE:<br> case DownloadRecord.STATE_PENDING:<br> case DownloadRecord.STATE_RUNNING:<br> case DownloadRecord.STATE_PAUSE:<br> // 关闭通知栏<br> notificationManager.cancel(100);<br> break;<br> case DownloadRecord.STATE_SUCCESS:<br> // 显示安装确认弹窗<br> showInstallAlert(true);<br> break;<br> case DownloadRecord.STATE_FAILURE:<br> // 重新下载<br> download(mContext, this.downloadUrl, this.savePath);<br> break;<br> default:<br> break;<br> }<br> }<br> }<br> }<br>} |
以上就是直播平台源码,通知栏中显示滑动的进度条实现的相关代码, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现