在进行软件升级时,需要进行文件下载,在这里实现自定义的文件下载,并在状态栏显示下载进度,下载完成后,点击触发安装。
效果如图:
用于下载文件和显示现在进度的线程类如下:
-
packagecom.channelsoft.ahzyfis.util;
-
-
importjava.io.File;
-
importjava.io.FileOutputStream;
-
importjava.io.InputStream;
-
importjava.net.HttpURLConnection;
-
importjava.net.URL;
-
-
importandroid.app.Notification;
-
importandroid.app.NotificationManager;
-
importandroid.app.PendingIntent;
-
importandroid.content.Context;
-
importandroid.content.Intent;
-
importandroid.net.Uri;
-
importandroid.os.Environment;
-
importandroid.os.Handler;
-
importandroid.os.Message;
-
importandroid.util.Log;
-
importandroid.widget.RemoteViews;
-
importandroid.widget.Toast;
-
-
importcom.channelsoft.ahzyfis.AhzyFisActivity;
-
importcom.channelsoft.ahzyfis.R;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
publicclassAppFileDownUtilsextendsThread{
-
-
privateContextmContext;
-
privateHandlermHandler;
-
privateStringmDownloadUrl;
-
privateStringmFileName;
-
privateMessagemsg;
-
-
privatefinalStringAPP_FOLDER="DownDemo";
-
privatefinalStringAPK_FOLDER="apkFile";
-
-
publicstaticfinalintMSG_UNDOWN=0;
-
publicstaticfinalintMSG_DOWNING=1;
-
publicstaticfinalintMSG_FINISH=1;
-
publicstaticfinalintMSG_FAILURE=2;
-
-
privateNotificationManagermNotifManager;
-
privateNotificationmDownNotification;
-
privateRemoteViewsmContentView;
-
privatePendingIntentmDownPendingIntent;
-
-
publicAppFileDownUtils(Contextcontext,Handlerhandler,
-
StringdownloadUrl,StringfileName){
-
mContext=context;
-
mHandler=handler;
-
mDownloadUrl=downloadUrl;
-
mFileName=fileName;
-
mNotifManager=(NotificationManager)mContext
-
.getSystemService(Context.NOTIFICATION_SERVICE);
-
msg=newMessage();
-
}
-
-
@Override
-
publicvoidrun(){
-
try{
-
if(Environment.getExternalStorageState().equals(
-
Environment.MEDIA_MOUNTED)){
-
MessagedowningMsg=newMessage();
-
downingMsg.what=MSG_DOWNING;
-
mHandler.sendMessage(downingMsg);
-
-
FilesdcardDir=Environment.getExternalStorageDirectory();
-
-
Filefolder=newFile(sdcardDir+File.separator+APP_FOLDER
-
+File.separator+APK_FOLDER);
-
if(!folder.exists()){
-
-
folder.mkdir();
-
}
-
FilesaveFilePath=newFile(folder,mFileName);
-
System.out.println(saveFilePath);
-
mDownNotification=newNotification(
-
android.R.drawable.stat_sys_download,mContext
-
.getString(R.string.notif_down_file),System
-
.currentTimeMillis());
-
mDownNotification.flags=Notification.FLAG_ONGOING_EVENT;
-
mDownNotification.flags=Notification.FLAG_AUTO_CANCEL;
-
mContentView=newRemoteViews(mContext.getPackageName(),
-
R.layout.custom_notification);
-
mContentView.setImageViewResource(R.id.downLoadIcon,
-
android.R.drawable.stat_sys_download);
-
mDownPendingIntent=PendingIntent.getActivity(mContext,0,newIntent(),0);
-
booleandownSuc=downloadFile(mDownloadUrl,saveFilePath);
-
if(downSuc){
-
msg.what=MSG_FINISH;
-
Notificationnotification=newNotification(
-
android.R.drawable.stat_sys_download_done,mContext
-
.getString(R.string.downloadSuccess),
-
System.currentTimeMillis());
-
notification.flags=Notification.FLAG_ONGOING_EVENT;
-
notification.flags=Notification.FLAG_AUTO_CANCEL;
-
Intentintent=newIntent(Intent.ACTION_VIEW);
-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(Uri.fromFile(saveFilePath),
-
"application/vnd.android.package-archive");
-
PendingIntentcontentIntent=PendingIntent.getActivity(
-
mContext,0,intent,0);
-
notification.setLatestEventInfo(mContext,mContext
-
.getString(R.string.downloadSuccess),null,
-
contentIntent);
-
mNotifManager.notify(R.drawable.icon,notification);
-
}else{
-
msg.what=MSG_FAILURE;
-
Notificationnotification=newNotification(
-
android.R.drawable.stat_sys_download_done,mContext
-
.getString(R.string.downloadFailure),
-
System.currentTimeMillis());
-
notification.flags=Notification.FLAG_AUTO_CANCEL;
-
PendingIntentcontentIntent=PendingIntent.getActivity(
-
mContext,0,newIntent(),0);
-
notification.setLatestEventInfo(mContext,mContext
-
.getString(R.string.downloadFailure),null,
-
contentIntent);
-
mNotifManager.notify(R.drawable.icon,notification);
-
}
-
-
}else{
-
Toast.makeText(mContext,Environment.getExternalStorageState(),
-
Toast.LENGTH_SHORT).show();
-
msg.what=MSG_FAILURE;
-
}
-
}catch(Exceptione){
-
Log.e(AhzyFisActivity.TAG,"AppFileDownUtilscatchException:",e);
-
msg.what=MSG_FAILURE;
-
}finally{
-
mHandler.sendMessage(msg);
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
-
publicbooleandownloadFile(StringdownloadUrl,FilesaveFilePath){
-
intfileSize=-1;
-
intdownFileSize=0;
-
booleanresult=false;
-
intprogress=0;
-
try{
-
URLurl=newURL(downloadUrl);
-
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
-
if(null==conn){
-
returnfalse;
-
}
-
-
conn.setReadTimeout(10000);
-
conn.setRequestMethod("GET");
-
conn.setDoInput(true);
-
conn.connect();
-
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
-
fileSize=conn.getContentLength();
-
InputStreamis=conn.getInputStream();
-
FileOutputStreamfos=newFileOutputStream(saveFilePath);
-
byte[]buffer=newbyte[1024];
-
inti=0;
-
inttempProgress=-1;
-
while((i=is.read(buffer))!=-1){
-
downFileSize=downFileSize+i;
-
-
progress=(int)(downFileSize*100.0/fileSize);
-
fos.write(buffer,0,i);
-
-
synchronized(this){
-
if(downFileSize==fileSize){
-
-
mNotifManager.cancel(R.id.downLoadIcon);
-
}elseif(tempProgress!=progress){
-
-
mContentView.setTextViewText(R.id.progressPercent,
-
progress+"%");
-
mContentView.setProgressBar(R.id.downLoadProgress,
-
100,progress,false);
-
mDownNotification.contentView=mContentView;
-
mDownNotification.contentIntent=mDownPendingIntent;
-
mNotifManager.notify(R.id.downLoadIcon,
-
mDownNotification);
-
tempProgress=progress;
-
}
-
}
-
}
-
fos.flush();
-
fos.close();
-
is.close();
-
result=true;
-
}else{
-
result=false;
-
}
-
}catch(Exceptione){
-
result=false;
-
Log.e(AhzyFisActivity.TAG,"downloadFilecatchException:",e);
-
}
-
returnresult;
-
}
-
}
在下载过程中,如果需要和主线程(UI Main Thread)通信,及时让主线程了解下载进度和状态,可用通过Handle向主线程发送Message
进度条显示的布局文件如下:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayout
-
android:id="@+id/custom_notification"
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="horizontal"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
<ImageView
-
android:id="@+id/downLoadIcon"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginLeft="5dip"
-
android:layout_gravity="center_vertical"
-
/>
-
<TextView
-
android:layout_height="fill_parent"
-
android:layout_width="wrap_content"
-
android:layout_marginLeft="5dip"
-
android:text="@string/downloadProgress"
-
android:gravity="center_vertical"
-
/>
-
<ProgressBar
-
android:id="@+id/downLoadProgress"
-
style="?android:attr/progressBarStyleHorizontal"
-
mce_style="?android:attr/progressBarStyleHorizontal"
-
android:layout_marginLeft="10dip"
-
android:layout_width="150dip"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center_vertical"
-
/>
-
<TextView
-
android:id="@+id/progressPercent"
-
android:layout_height="fill_parent"
-
android:layout_width="wrap_content"
-
android:layout_marginLeft="5dip"
-
android:gravity="center_vertical"
-
/>
-
</LinearLayout>
分享到:
相关推荐
余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...
余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...
余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...
余志龙、陈昱勋、郑名杰、陈小风,分别来自手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及12EE、JSP、Servlet、Java Bean、PHP、C#等程序语言...
全书共分11章,主要以范例集的方式来讲述android的知识点,详细介绍了开发android的人机交互界面、android常用的开发控件、android手机收发短信等通信服务、开发android手机的自动服务功能和娱乐多媒体功能以及...
全书共分11章,主要以范例集的方式来讲述Android的知识点,详细介绍了开发Android的人机交互界面、Android常用的开发控件、Android手机收发短信等通信服务、开发Android手机的自动服务功能和娱乐多媒体功能以及...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
8.14 远程下载安装Android程序——APKInstaller的应用 8.15 手机下载看3gp影片——Runnable混搭SurfaceView 8.16 访问网站LoginAPI——远程服务器验证程序运行权限 8.17 地震速报!——HttpURLConnection与Service...
google android sdk开发范例大全 第二版 PDF 和 随书光盘代码 ISBN:9787115229649 目录 第1章 了解、深入、动手做 1.1 红透半边天的Android 1.2 本书目的及范例涵盖范围 1.3 如何阅读本书 ...
Google Android SDK开发范例大全(完整版) 包含部分书中源码 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 ...
余志龙,来自于手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及J2EE、JSP、Servlet、JavaBeans、PHP、C#等程序语言,熟悉面向对象技术与Eclipse...
1、ANDROID文件系统与应用程序架构 7 1.1、ANDROID 文件系统 7 1.2、ANDROID应用程序架构 9 2、ANDROID应用程序结构 11 2.1、ACTIVITY 12 2.1.1、概述 12 2.1.2、Activity的生命周期 15 2.1.3、Activity 的创建 16 ...
1、ANDROID文件系统与应用程序架构 7 1.1、ANDROID 文件系统 7 1.2、ANDROID应用程序架构 9 2、ANDROID应用程序结构 11 2.1、ACTIVITY 12 2.1.1、概述 12 2.1.2、Activity的生命周期 15 2.1.3、Activity 的创建 16 ...
Google Android SDK开发范例大全 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 安装AndroidSDK与...