Android 如何在代码中添加进度通知以供下载
我已在此应用程序中创建了一个应用程序我已显示pdf文件,当用户单击时,文件应下载。我已编写下载代码,我只能显示下载进度对话框,但我希望使用“取消”按钮通知进度。我不知道我怎么能做到 这是我的下载代码Android 如何在代码中添加进度通知以供下载,android,Android,我已在此应用程序中创建了一个应用程序我已显示pdf文件,当用户单击时,文件应下载。我已编写下载代码,我只能显示下载进度对话框,但我希望使用“取消”按钮通知进度。我不知道我怎么能做到 这是我的下载代码 public class DownloadTask { private static final String TAG = "Download Task"; private Context context; private String downloadUrl = "",
public class DownloadTask {
private static final String TAG = "Download Task";
private Context context;
private String downloadUrl = "", downloadFileName = "";
private ProgressDialog progressDialog;
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public DownloadTask(Context context, String downloadUrl, String downloadFileName) {
this.context = context;
this.downloadUrl = downloadUrl;
this.downloadFileName =downloadFileName;
new DownloadingTask().execute();
}
private class DownloadingTask extends AsyncTask<Void, Void, Void> {
File apkStorage = null;
File outputFile = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected void onPostExecute(Void result) {
try {
if (outputFile != null) {
progressDialog.dismiss();
Toast.makeText(context, "Downloaded Successfully", Toast.LENGTH_SHORT).show();
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ "android"+"/"+"data"+"/"+"FolderName"+"/"+ downloadFileName);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
if (uri.toString().contains(".pdf")) {
intent.setDataAndType(uri, "application/pdf");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, 3000);
Log.e(TAG, "Download Failed");
}
} catch (Exception e) {
e.printStackTrace();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, 3000);
Log.e(TAG, "Download Failed with Exception - " + e.getLocalizedMessage());
}
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(downloadUrl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();//connect the URL Connection
if (c.getResponseCode() !=
HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = new File(
Environment.getExternalStorageDirectory() + "/"
+ "android"+"/"+"data"+"/"+"Folder name");
} else
Toast.makeText(context, "Oops!! There is no SD Card.", Toast.LENGTH_SHORT).show();
if (!apkStorage.exists()) {
apkStorage.mkdir();
Log.e(TAG, "Directory Created.");
}
outputFile = new File(apkStorage, downloadFileName);
if (!outputFile.exists()) {
outputFile.createNewFile();
Log.e(TAG, "File Created");
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);//Write new file
}
fos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
outputFile = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;
}
}
公共类下载任务{
私有静态最终字符串TAG=“下载任务”;
私人语境;
私有字符串downloadUrl=“”,downloadFileName=“”;
私有进程对话;
@RequiresApi(api=Build.VERSION\u code.CUPCAKE)
公共下载任务(上下文上下文、字符串下载URL、字符串下载文件名){
this.context=上下文;
this.downloadUrl=下载URL;
this.downloadFileName=下载文件名;
新建DownloadingTask().execute();
}
私有类下载任务扩展异步任务{
文件apkStorage=null;
File outputFile=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(上下文);
progressDialog.setMessage(“加载…”);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(假);
progressDialog.show();
}
@凌驾
受保护的void onPostExecute(void结果){
试一试{
if(outputFile!=null){
progressDialog.disclose();
Toast.makeText(上下文,“下载成功”,Toast.LENGTH_SHORT.show();
File File=新文件(Environment.getExternalStorageDirectory()+“/”
+“android”+“/”+“数据”+“/”+“文件夹名”+“/”+下载文件名);
StrictMode.VmPolicy.Builder=新的StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Uri=Uri.fromFile(文件);
意向意向=新意向(意向.行动\视图);
if(uri.toString()包含(“.pdf”)){
setDataAndType(uri,“应用程序/pdf”);
}
intent.addFlags(intent.FLAG\u活动\u新任务);
背景。开始触觉(意图);
}否则{
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
}
}, 3000);
Log.e(标记“下载失败”);
}
}捕获(例外e){
e、 printStackTrace();
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
}
}, 3000);
Log.e(标记“下载失败,出现异常-”+e.getLocalizedMessage());
}
super.onPostExecute(结果);
}
@凌驾
受保护的Void doInBackground(Void…arg0){
试一试{
URL=新URL(下载URL);
HttpURLConnection c=(HttpURLConnection)url.openConnection();
c、 setRequestMethod(“GET”);
c、 connect();//连接URL连接
如果(c.getResponseCode()!=
HttpURLConnection.HTTP_(确定){
Log.e(标记,“服务器返回HTTP”+c.getResponseCode()
+“+c.getResponseMessage());
}
if(新的CheckForSDCard().isSDCardPresent()){
apkStorage=新文件(
环境。getExternalStorageDirectory()+“/”
+“android”+“/”+“数据”+“/”+“文件夹名称”);
}否则
Toast.makeText(上下文,“Oops!!没有SD卡。”,Toast.LENGTH\u SHORT.show();
如果(!apkStorage.exists()){
apkStorage.mkdir();
Log.e(标记“目录已创建”);
}
outputFile=新文件(apkStorage,downloadFileName);
如果(!outputFile.exists()){
outputFile.createNewFile();
Log.e(标记“文件已创建”);
}
FileOutputStream fos=新的FileOutputStream(outputFile);
InputStream=c.getInputStream();
字节[]缓冲区=新字节[1024];//设置缓冲区类型
int len1=0;//初始长度
而((len1=is.read(buffer))!=-1){
fos.write(缓冲区,0,len1);//写入新文件
}
fos.close();
is.close();
}捕获(例外e){
e、 printStackTrace();
outputFile=null;
Log.e(标记“下载错误异常”+e.getMessage());
}
返回null;
}
}
在下载任务的doInBackground
方法中,定期调用publishProgress
将进度传输到您的UI,然后在UI线程上执行的onProgressUpdate
中更新进度条,因此可以在对话框中显示进度条。我想要进度通知操作BART然后更改进度通知栏的当前值,而不是更改onProgressUpdate
中的ProgressBar
的当前值。