当前位置:操作系统 > 安卓/Android >>

Android开发之文件下载,状态时显示下载进度,点击自动安装

 

在进行软件升级时,需要进行文件下载,在这里实现自定义的文件下载,并在状态栏显示下载进度,下载完成后,点击触发安装。

 

用于下载文件和显示现在进度的线程类如下:

 

 

package com.channelsoft.ahzyfis.util; 

 

import java.io.File; 

import java.io.FileOutputStream; 

import java.io.InputStream; 

import java.net.HttpURLConnection; 

import java.net.URL; 

 

import android.app.Notification; 

import android.app.NotificationManager; 

import android.app.PendingIntent; 

import android.content.Context; 

import android.content.Intent; 

import android.net.Uri; 

import android.os.Environment; 

import android.os.Handler; 

import android.os.Message; 

import android.util.Log; 

import android.widget.RemoteViews; 

import android.widget.Toast; 

 

import com.channelsoft.ahzyfis.AhzyFisActivity; 

import com.channelsoft.ahzyfis.R; 

 

/**

 * 

 * <dl>

 * <dt>AppFileDownUtils.java</dt>

 * <dd>Description: 文件下载</dd>

 * <dd>Copyright: Copyright (C) 2011</dd>

 * <dd>Company: </dd>

 * <dd>CreateDate: 2011-10-19</dd>

 * </dl>

 * 

 * @author ZhanHua

 */ 

public class AppFileDownUtils extends Thread { 

 

    private Context mContext; 

    private Handler mHandler; 

    private String mDownloadUrl; // 文件下载url,已做非空检查 

    private String mFileName; 

    private Message msg; 

 

    private final String APP_FOLDER = "DownDemo"; // sd卡应用目录 

    private final String APK_FOLDER = "apkFile"; // 下载apk文件目录 

 

    public static final int MSG_UNDOWN = 0; //未开始下载 

    public static final int MSG_DOWNING = 1; // 下载中 

    public static final int MSG_FINISH = 1; // 下载完成 

    public static final int MSG_FAILURE = 2;// 下载失败 

 

    private NotificationManager mNotifManager; 

    private Notification mDownNotification; 

    private RemoteViews mContentView; // 下载进度View 

    private PendingIntent mDownPendingIntent; 

 

    public AppFileDownUtils(Context context, Handler handler, 

            String downloadUrl, String fileName) { 

        mContext = context; 

        mHandler = handler; 

        mDownloadUrl = downloadUrl; 

        mFileName = fileName; 

        mNotifManager = (NotificationManager) mContext 

                .getSystemService(Context.NOTIFICATION_SERVICE); 

        msg = new Message(); 

    } 

 

    @Override 

    public void run() { 

        try { 

            if (Environment.getExternalStorageState().equals( 

                    Environment.MEDIA_MOUNTED)) { 

                Message downingMsg = new Message(); 

                downingMsg.what = MSG_DOWNING; 

                mHandler.sendMessage(downingMsg); 

                // SD卡准备好 

                File sdcardDir = Environment.getExternalStorageDirectory(); 

                // 文件存放路径:sdcard/DownDemo/apkFile 

                File folder = new File(sdcardDir + File.separator + APP_FOLDER 

                        + File.separator + APK_FOLDER); 

                if (!folder.exists()) { 

                    //创建存放目录 

                    folder.mkdir(); 

                } 

                File saveFilePath = new File(folder, mFileName); 

                System.out.println(saveFilePath); 

                mDownNotification = new Notification( 

                        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 = new RemoteViews(mContext.getPackageName(), 

                        R.layout.custom_notification); 

        &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,