Android DownloadManager下载状态查询(2)

2023-12-22 22:32

本文主要是介绍Android DownloadManager下载状态查询(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



Android DownloadManager下载状态查询(2)

在我写的前一篇文章中,
《Android大数据、断点续传、耗时下载之DownloadManager开发简介(1)》
文章链接地址:http://blog.csdn.net/zhangphil/article/details/48949027 
大致简介了Android DownloadManager如何完成一个下载任务。这篇文章在前一篇文章的基础上,做一些小改动,增加对下载任务状态的查询。
现在给出全部源代码。
MainActivity.Java文件:

[java]  view plain copy
  1. package zhangphil.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.DownloadManager;  
  5. import android.app.DownloadManager.Request;  
  6. import android.content.Context;  
  7. import android.database.Cursor;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private DownloadManager downloadManager;  
  18.     private long Id;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         Button button = (Button) findViewById(R.id.button);  
  25.         button.setOnClickListener(new View.OnClickListener() {  
  26.   
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 // remove将依据Id号取消相应的下载任务  
  30.                 // 可批量取消,remove(id1,id2,id3,id4,...);  
  31.                 downloadManager.remove(Id);  
  32.             }  
  33.         });  
  34.   
  35.         downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);  
  36.   
  37.         // 假设从这一个链接下载一个大文件。  
  38.         Request request = new Request(  
  39.                 Uri.parse("http://apkc.mumayi.com/2015/03/06/92/927937/xingxiangyi_V3.1.3_mumayi_00169.apk"));  
  40.   
  41.         // 仅允许在WIFI连接情况下下载  
  42.         request.setAllowedNetworkTypes(Request.NETWORK_WIFI);  
  43.   
  44.         // 通知栏中将出现的内容  
  45.         request.setTitle("我的下载");  
  46.         request.setDescription("下载一个大文件");  
  47.         // 下载过程和下载完成后通知栏有通知消息。  
  48.         request.setNotificationVisibility(Request.VISIBILITY_VISIBLE | Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
  49.   
  50.         // 此处可以由开发者自己指定一个文件存放下载文件。  
  51.         // 如果不指定则Android将使用系统默认的  
  52.         // request.setDestinationUri(Uri.fromFile(new File("")));  
  53.   
  54.         // 默认的Android系统下载存储目录  
  55.         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my.apk");  
  56.   
  57.         // enqueue 开始启动下载...  
  58.         Id = downloadManager.enqueue(request);  
  59.   
  60.         Button queryButton = (Button) findViewById(R.id.queryButton);  
  61.         queryButton.setOnClickListener(new View.OnClickListener() {  
  62.   
  63.             @Override  
  64.             public void onClick(View v) {  
  65.                 queryStatus();  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     // 根据DownloadManager下载的Id,查询DownloadManager某个Id的下载任务状态。  
  71.     private void queryStatus() {  
  72.         DownloadManager.Query query = new DownloadManager.Query();  
  73.         query.setFilterById(Id);  
  74.         Cursor cursor = downloadManager.query(query);  
  75.   
  76.         String statusMsg = "";  
  77.         if (cursor.moveToFirst()) {  
  78.             int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));  
  79.             switch (status) {  
  80.             case DownloadManager.STATUS_PAUSED:  
  81.                 statusMsg = "STATUS_PAUSED";  
  82.             case DownloadManager.STATUS_PENDING:  
  83.                 statusMsg = "STATUS_PENDING";  
  84.             case DownloadManager.STATUS_RUNNING:  
  85.                 statusMsg = "STATUS_RUNNING";  
  86.                 break;  
  87.             case DownloadManager.STATUS_SUCCESSFUL:  
  88.                 statusMsg = "STATUS_SUCCESSFUL";  
  89.                 break;  
  90.             case DownloadManager.STATUS_FAILED:  
  91.                 statusMsg = "STATUS_FAILED";  
  92.                 break;  
  93.   
  94.             default:  
  95.                 statusMsg = "未知状态";  
  96.                 break;  
  97.             }  
  98.   
  99.             Toast.makeText(getApplicationContext(), statusMsg, Toast.LENGTH_SHORT).show();  
  100.         }  
  101.     }  
  102. }  


MainActivity.java需要的布局文件activity_main.xml:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="取消下载" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/queryButton"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="查询下载状态" />  
  19.   
  20. </LinearLayout>  

这篇关于Android DownloadManager下载状态查询(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/525705

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

常用的jdk下载地址

jdk下载地址 安装方式可以看之前的博客: mac安装jdk oracle 版本:https://www.oracle.com/java/technologies/downloads/ Eclipse Temurin版本:https://adoptium.net/zh-CN/temurin/releases/ 阿里版本: github:https://github.com/

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

状态dp总结

zoj 3631  N 个数中选若干数和(只能选一次)<=M 的最大值 const int Max_N = 38 ;int a[1<<16] , b[1<<16] , x[Max_N] , e[Max_N] ;void GetNum(int g[] , int n , int s[] , int &m){ int i , j , t ;m = 0 ;for(i = 0 ;

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

hdu3006状态dp

给你n个集合。集合中均为数字且数字的范围在[1,m]内。m<=14。现在问用这些集合能组成多少个集合自己本身也算。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.Inp

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动