Android开发之文件下载(通知栏中显示进度条,状态信息等)
来源:互联网 发布:java员工管理系统 ssh 编辑:程序博客网 时间:2024/05/16 04:44
Android开发之文件下载(通知栏中显示进度条,状态信息等)
文件下载一直都是开发者开发过程中不可或缺的一个环节,前几天下载文件,发现在通知栏显示下载进度的效果非常炫酷,本人热衷于研究,故经一番研究后,终于实现了其基本的效果,现将代码贴出,望各位大神批评指正,灰常感谢♪(^∇^*)
本人模拟的是下载携程旅行的apk文件,并且下载完自动跳转到安装目录的Demo,我们废话少说,直接贴代码吧!
先上效果图:
核心代码:
MainActivity
package com.hxht.testdownloadapk;import android.app.Notification;import android.app.NotificationManager;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.RemoteViews;import android.widget.Toast;import net.tsz.afinal.FinalHttp;import net.tsz.afinal.http.AjaxCallBack;import java.io.File;public class MainActivity extends AppCompatActivity { private String url; private String fileSavePath; private NotificationManager manager; private Notification mNotification; private static final int pushId = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); } /** * 设置数据 */ private void initData() { url = "http://www.apk.anzhi.com/data3/apk/201509/08/1c7406573baafcff69ddb4fbd3e444d3_08425600.apk"; fileSavePath = Environment.getExternalStorageDirectory().getPath() + "/cuibo2B.apk"; manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); mNotification = new Notification(R.drawable.icon02, "新版本版本来袭,速速下载吧", System.currentTimeMillis()); mNotification.contentView = new RemoteViews("com.hxht.testdownloadapk", R.layout.activity_update_message); mNotification.flags = Notification.FLAG_NO_CLEAR; //mNotification.defaults = Notification.DEFAULT_ALL ; } /** * 按钮点击事件 * * @param view */ public void BeginDownloadApk(View view) { FinalHttp mFinalHttp = new FinalHttp(); File file = new File(fileSavePath); if (file.exists()) { file.delete(); } mFinalHttp.download(url, fileSavePath, new AjaxCallBack<File>() { /** * 正在下载 * @param count * @param current */ @Override public void onLoading(long count, long current) { super.onLoading(count, current); int progress = (int) (current * 1.0f / count * 100); if (progress % 5 == 0) { mNotification.contentView.setTextViewText(R.id.tv_desc, "正在下载中."); } else if (progress % 5 == 1) { mNotification.contentView.setTextViewText(R.id.tv_desc, "正在下载中.."); } else if (progress % 5 == 2) { mNotification.contentView.setTextViewText(R.id.tv_desc, "正在下载中..."); } else if (progress % 5 == 3) { mNotification.contentView.setTextViewText(R.id.tv_desc, "正在下载中...."); } else if (progress % 5 == 4) { mNotification.contentView.setTextViewText(R.id.tv_desc, "正在下载中....."); } mNotification.contentView.setTextViewText(R.id.tv_progress, progress + "/100"); mNotification.contentView.setProgressBar(R.id.pb, 100, progress, false); manager.notify(pushId, mNotification); } /** * 下载成功 * @param file */ @Override public void onSuccess(File file) { super.onSuccess(file); Toast.makeText(MainActivity.this, "下载成功,文件保存在了" + fileSavePath + "路径下", Toast.LENGTH_SHORT).show(); mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotification.contentView.setTextViewText(R.id.tv_desc, "下载成功"); mNotification.contentView.setTextViewText(R.id.tv_progress, ""); mNotification.contentView.setProgressBar(R.id.pb, 100, 100, false); manager.notify(pushId, mNotification); installAPK(file); } /** * 下载失败 * @param t * @param strMsg */ @Override public void onFailure(Throwable t, String strMsg) { super.onFailure(t, strMsg); Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotification.contentView.setTextViewText(R.id.tv_desc, "下载失败,连接服务器中..."); mNotification.contentView.setTextViewText(R.id.tv_progress, ""); mNotification.contentView.setProgressBar(R.id.pb, 100, 100, true); manager.notify(pushId, mNotification); } }); } private void installAPK(File file) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); }}
代码已贴出,正所谓取之于社会,回报于社会,望各路大神批评指正,不喜勿喷,灰常感谢♪(^∇^*)!!!
0 0
- Android开发之文件下载(通知栏中显示进度条,状态信息等)
- Android 下载文件通知栏显示进度条
- 【Android】下载文件:进度条和通知栏
- Android 开发 根据activity前台状态显示通知栏信息或对话框(推送/本地service)
- Android 下载文件 进度条显示
- Android 下载文件 显示进度条
- Android 下载文件 显示进度条
- Android 下载文件 进度条显示
- android 下载通知栏中进度条的制作
- Android 在通知中显示进度条
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android带下载进度条的通知栏
- Android 下载文件并显示进度条
- Android中检测更新、通知、下载等
- Android学习笔记19:带有通知栏的进度条的Android下载文件
- 文件下载显示进度条
- notify——通知栏显示下载图片的进度条
- (转) unity 在移动平台中,文件操作路径详解
- Maven 依赖管理
- 对xml增删改查,格式化与拓展
- 替换字符串里的空格与基本字符串压缩
- Docker可以用来做什么?
- Android开发之文件下载(通知栏中显示进度条,状态信息等)
- 在需求分析中就可以避免的那些错误12
- 【转】 关于ExpandableListView用法的一个简单小例子
- Codeforces Round #320 (Div. 2) B - Finding Team Member
- 用Python开发自动化测试脚本
- 毛玻璃的特效用法
- Android StudioでAndroidAnnotations3.0.1を使う方法
- UIScrollView的一些特点
- finished with non-zero exit value 2