如何在android 2.2中创建自己的下载管理器

如何在android 2.2中创建自己的下载管理器,android,Android,我知道我们可以在安卓2.3及以上版本中使用内置下载管理器,但我的应用程序适合安卓2.2及以上版本。我的问题是如何在安卓2.2中创建自己的下载管理器?请给我一些示例答案 请给我一些示例答案 步骤1查看如何在Android中下载文件的示例 步骤2查看如何在AsyncTask中执行操作的示例 步骤3查看下载时如何显示下载进度的示例 步骤4查看任务完成时如何发送自定义广播的示例 步骤5查看如何即使在设备旋转的情况下仍保持AsysncTask操作的示例 步骤6查看如何在通知中显示下载进度的示例 下面是示例

我知道我们可以在安卓2.3及以上版本中使用内置下载管理器,但我的应用程序适合安卓2.2及以上版本。我的问题是如何在安卓2.2中创建自己的下载管理器?请给我一些示例答案

请给我一些示例答案

步骤1查看如何在Android中下载文件的示例

步骤2查看如何在AsyncTask中执行操作的示例

步骤3查看下载时如何显示下载进度的示例

步骤4查看任务完成时如何发送自定义广播的示例

步骤5查看如何即使在设备旋转的情况下仍保持AsysncTask操作的示例

步骤6查看如何在通知中显示下载进度的示例

下面是示例代码

1。使用AsyncTask并在对话框中显示下载进度

// declare the dialog as a member field of your activity
ProgressDialog mProgressDialog;

// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("the url to the file you want to download");

The AsyncTask will look like this:

// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/file_name.extension");

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
        }
        return null;
    }
2。从服务下载

这里的大问题是:如何从服务更新我的活动?。在下一个示例中,我们将使用两个您可能不知道的类:ResultReceiver和IntentService。ResultReceiver允许我们从服务更新线程;IntentService是服务的一个子类,它生成一个线程来执行后台工作(您应该知道服务实际上在应用程序的同一线程中运行;当您扩展服务时,必须手动生成新线程来运行CPU阻塞操作)

下载服务可以如下所示:

public class DownloadService extends IntentService {
    public static final int UPDATE_PROGRESS = 8344;
    public DownloadService() {
        super("DownloadService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        String urlToDownload = intent.getStringExtra("url");
        ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
        try {
            URL url = new URL(urlToDownload);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk");

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                Bundle resultData = new Bundle();
                resultData.putInt("progress" ,(int) (total * 100 / fileLength));
                receiver.send(UPDATE_PROGRESS, resultData);
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Bundle resultData = new Bundle();
        resultData.putInt("progress" ,100);
        receiver.send(UPDATE_PROGRESS, resultData);
    }
}
将服务添加到您的清单:

<service android:name=".DownloadService"/>
以下是ResultReceiver来玩的结果:

private class DownloadReceiver extends ResultReceiver{
    public DownloadReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        if (resultCode == DownloadService.UPDATE_PROGRESS) {
            int progress = resultData.getInt("progress");
            mProgressDialog.setProgress(progress);
            if (progress == 100) {
                mProgressDialog.dismiss();
            }
        }
    }
}

谢谢你的回复。我会试试这个。如果它解决了你的问题,别忘了接受答案,因为它会帮助其他用户,
mProgressDialog.show();
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("url", "url of the file to download");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);
private class DownloadReceiver extends ResultReceiver{
    public DownloadReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        if (resultCode == DownloadService.UPDATE_PROGRESS) {
            int progress = resultData.getInt("progress");
            mProgressDialog.setProgress(progress);
            if (progress == 100) {
                mProgressDialog.dismiss();
            }
        }
    }
}