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);
}
}
代码已贴出,正所谓取之于社会,回报于社会,望各路大神批评指正,不喜勿喷,灰常感谢♪(^∇^*)!!!