Android实现Service下载文件,Notification显示下载进度的示例

先放个gif。。最终效果如果:

主要演示了Android从服务器下载文件,调用Notification显示下载进度,并且在下载完毕以后点击通知会跳转到安装APK的界面,演示是在真实的网络环境中使用真实的URL进行演示,来看看代码:

MainActivity代码非常简单,就是启动一个Service:

public class MainActivity extends AppCompatActivity { String download_url="http://shouji.360tpcdn.com/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.PicFolder_4722404.apk"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void bt_start_service(View view){ Intent intent=new Intent(this,DownLoadService.class); intent.putExtra("download_url",download_url); startService(intent); } }

DownLoadService里面,在onStartCommand方法里面是关键代码,调用NotifyUtil这个工具类的“notify_progress”方法去显示一个通知,与此同时开始下载APK文件,DownLoadService代码如下:

public class DownLoadService extends Service { String download_url; String savePath= Environment.getExternalStorageDirectory()+"/liulan.apk"; private int requestCode = (int) SystemClock.uptimeMillis(); private NotifyUtil currentNotify; File mFile; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { mFile=new File(savePath); download_url=intent.getStringExtra("download_url"); Log.e("test","执行onStartCommand"); //设置想要展示的数据内容 Intent intent_noti = new Intent(); intent_noti.setAction(Intent.ACTION_VIEW); //文件的类型,从tomcat里面找 intent_noti.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive"); PendingIntent rightPendIntent = PendingIntent.getActivity(this, requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT); int smallIcon = R.drawable.xc_smaillicon; String ticker = "正在更新快图浏览"; //实例化工具类,并且调用接口 NotifyUtil notify7 = new NotifyUtil(this, 7); notify7.notify_progress(rightPendIntent, smallIcon, ticker, "快图浏览升级程序", "正在下载中", false, false, false, download_url, savePath, new NotifyUtil.DownLoadListener() { @Override public void OnSuccess(File file) { mFile=file; DownLoadService.this.stopSelf(); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { } }); currentNotify = notify7; return super.onStartCommand(intent, flags, startId); } }

在调用“notify_progress”方法的时候,已经开始下载文件了,那么下载的代码是什么呢?如下:

public void notify_progress(PendingIntent pendingIntent, int smallIcon, String ticker, String title, String content, boolean sound, boolean vibrate, boolean lights, String download_url, String savePath, final DownLoadListener listener) { setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights); /* * 因为进度条要实时更新通知栏也就说要不断的发送新的提示,所以这里不建议开启通知声音。 * 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。 */ FinalHttp fh = new FinalHttp(); HttpHandler<File> httpHandler=fh.download(download_url, savePath, new AjaxCallBack<File>() { @Override public void onLoading(long count, long current) { super.onLoading(count, current); double a=count; double b=current; double currentPro=(double)((b/a)*100); cBuilder.setProgress(100, (int)currentPro, false); sent(); } @Override public void onSuccess(File file) { super.onSuccess(file); cBuilder.setContentText("下载完成").setProgress(0, 0, false); sent(); listener.OnSuccess(file); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { super.onFailure(t, errorNo, strMsg); listener.onFailure(t,errorNo,strMsg); } }); }

这里用到了afinal.jar

这个jar已经封装好下载的工具类,我们直接拿来用就行。下载成功之后会通过DownLoadListener这个接口回调到DownLoadService里面,最终运行效果就如最上面那个gif动态图运行效果一样。

项目下载地址:点击下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-07-31 09:08:48

Android实现Service下载文件,Notification显示下载进度的示例的相关文章

python下载文件时显示下载进度的方法_python

本文实例讲述了python下载文件时显示下载进度的方法.分享给大家供大家参考.具体分析如下: 将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report) 第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度 def report(count, blockSize, totalSize): percent = int(count*blockSize*10

asp.net WinForm下载文件并显示下载进度条教程

WinForm下载文件并显示下载进度示例  代码如下 复制代码 /// <summary> /// 显示进度 /// </summary> /// <param name="val"></param> private void ProgressBar_Value(int val) {     progressBar1.Value = val;     label1.Text = val.ToString() + "%"

python下载文件时显示下载进度的方法

 具体分析如下: 将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report) 第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度 1 2 3 4 5 6 7 8 def report(count, blockSize, totalSize): percent = int(count*blockSize*100/totalSize) sys.stdou

win7系统下载文件没有出现下载对话框怎么办

由于这个问题是跟另外一个xp系统出现这个问题是一样的所以我把那边的问题直接复制过来了.xp系统下载文件没有出现下载对话框 问题的具体描述:下载的文件通常是压缩文件,其他类型的文件小编没去尝试.因为这是一个朋友问我的问题,而下载完zip或RAR文件后,不止没下载对话框,让你选择保存的位置,下载完还会自动打开. 1 默认存放位置都是一样的,前提是你没把你的"我的文档"遇到其他位置 下载文件位置:C:WINDOWSsystem32configsystemprofileLocal Settin

xp系统下载文件没有出现下载对话框

xp系统下载文件没有出现下载对话框问题的具体描述:下载的文件通常是压缩文件,其他类型的文件小编没去尝试.因为这是一个朋友问我的问题,而下载完zip或RAR文件后,不止没下载对话框,让你选择保存的位置,下载完还会自动打开. 1 会问这个问题基本上有下载过文件了,这边先告诉你没弹窗对话框默认的保存位置 下载文件夹默认位置:C:WINDOWSsystem32configsystemprofileLocal SettingsTemporary Internet Files 2 更改配置 随便打开一个文件

link环境下使用codefirst技术制作《网盘软件》,下载器如何显示下载任务?

问题描述 link环境下使用codefirst技术制作<网盘软件>,下载器如何显示下载任务? link环境下使用codefirst技术制作<网盘软件>,下载器如何显示下载任务? 解决方案 参考:http://download.csdn.net/download/jhfant/4704284

asp.net用迅雷下载文件总是变成下载网页 ,有什么办法可以解决????????

问题描述 asp.net用迅雷下载文件总是变成下载网页,有什么办法可以解决?? 解决方案 解决方案二:这是迅雷监视了下载,么有办法吧解决方案三:你下载代码是不是有问题?解决方案四:直接链接文件就不会了解决方案五:代码没问题的,用IE下载就没问题,同时文件格式不是TXT也没问题解决方案六:直接链接文件具体怎样操作

JS下载文件|无刷新下载文件示例代码_javascript技巧

后台代码Handler.ashx 复制代码 代码如下: <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { string fileName = "web.conf

下载文件并显示进度条

  UINT DownloadFile(LPVOID pParam){ CWnd*   pwnd = AfxGetMainWnd(); CProgressCtrl* m_Prog = (CProgressCtrl*)pwnd->GetDlgItem(IDC_PROGRESS1); CButton*  bStart = (CButton*)pwnd->GetDlgItem(IDB_BTN_START);  char    filebuf[512]; CInternetSession netSes