如何创建事件下载完成后触发

时间:2013-11-23 17:30:41

标签: java android eventtrigger

我正在写一个downloadManager,我需要一个提示。在那里,我可以创建一个事件,如果我想要的话它就是火。但我似乎无法在java中找到如何做到这一点。我想要做的是我想为我的类创建一个事件,然后在其中一个类的成员方法中激活它。现在,当这个类被调用并看到下载完成时(例如某个变量已经达到100),它会触发一个指示情况的事件。我怎么能在java中创建它?    

public class DownloadManager
    {
        static Queue<AvailableGame> downloadQueue;
        static Integer currenProgress;
        static String currentDownload;
        static Boolean isRunning;


        /**
         * Start download
         */
        public void startDownloading()
        {

            AsyncTask task = new AsyncTask()
            {
                @Override
                protected Object doInBackground(Object[] objects)
                {
                    downloadNextFile();
                    return null;

        private static String downloadFile(String downloadUrl)
        {
            String toDownload = downloadUrl;
            String fileName = getFileNameFromUrl(toDownload);

            // take CPU lock to prevent CPU from going off if the user
            // presses the power button during download
            PowerManager pm = (PowerManager)
            MainActivity.getContext().getSystemService(Context.POWER_SERVICE); 
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "DownloadManager");
            wl.acquire();

            try
            {
                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;
                try
                {
                    URL url = new URL(toDownload);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();

                    // expect HTTP 200 OK, so we don't mistakenly save error report
                    // instead of the file
                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                    {
                        return "Server returned HTTP " + connection.getResponseCode() + " "
                        + connection.getResponseMessage();
                    }    

                    int fileLength = connection.getContentLength();

                    // download the file
                    input = connection.getInputStream();
                    output = new FileOutputStream("/sdcard/" + fileName);

                    byte data[] = new byte[4096];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1)
                    {
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0)
                        {
                            currenProgress = ((int) (total * 100 / fileLength));
                            currentDownload = "Downloading " + fileName;
                        }
                        output.write(data, 0, count);
                    }
                }
                catch (Exception e)
                {
                    return e.toString();
                }
                finally
                {
                    try
                    {
                        if (output != null)
                        {
                            output.close();
                        }
                        if (input != null)
                        {
                            input.close();
                        }
                    }
                    catch (IOException ignored)
                    {
                    }

                    if (connection != null)
                    {
                        connection.disconnect();
                    }
                }
            }
            finally
            {
                wl.release();
            }
            return null;
        }   


    }

  

如何在下载完成后保存信息,第二个问题是如何进行

1 个答案:

答案 0 :(得分:0)

首先,我认为您应该使用Android的DownloadManager。如果是,您可以注册一个BroadcastReceiver来检测下载完成时间。

您可以在手机上下载并运行完整的示例,其中包含完整的源代码,以便您了解如何执行此操作:https://github.com/commonsguy/cw-omnibus/tree/master/EmPubLite/T16-Update

专门检查文件DownloadCheckService.javaDownloadCompleteReceiver.java

希望它有所帮助。