开发者

onDownload complete notification - inapp purchase in Android

In my Android application I am downloading a video through an application purchase from Urban Airship.

When I click a particular button in my application the downloading gets started. In such a case, after the download completes fully I want to get a notification or alert so that the button becomes invisible. How can I know that the video has been downloaded completely?

Is there any piece of code I could write to be notified when a 开发者_StackOverflow中文版download finishes in Android?


    private void startDownload() {
        String url = PUT HERE YOUR URL;
        new DownloadFileAsync().execute(url);

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);

            // mProgressDialog.setIcon(R.drawable.icon);
            mProgressDialog.setIcon(R.drawable.tab_icon_pitchforkfm);
            mProgressDialog.setTitle("Downloading file... "
                    + GlobalVariable.Getstrpath().toString());

            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }

    class DownloadFileAsync extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        /**
         * This is used for downloading the file in the background
         * process.
         */
        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub.
            // mp3load();
            int len1 = 0;
            int count;
            try {
                URL url = new URL(GlobalVariable.Getstr());
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                int lenghtOfFile = c.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
                String PATH = Environment.getExternalStorageDirectory() +
                              "/download/";
                Log.v(LOG_TAG, "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                // String fileName = "workTest.mp3";
                String fileName = "TEST.mp3";

                File outputFile = new File(file, fileName);

                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                long total = 0;

                while ((count = is.read(buffer)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                    // fos.write(buffer, 0, len1);
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();

            } catch (IOException e) {
                Log.d(LOG_TAG, "Error: " + e);
            }
            return null;
        }

        protected void onProgressUpdate(String... progress) {
            Log.d("ANDRO_ASYNC", progress[0]);

            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            //refreshList();
            Toast.makeText(
                FindFilesByType.this,
                "Downloading of " + fileName + " complete.",
                Toast.LENGTH_LONG).show();

            //Put your code here. That is, button visible/invisible code, etc. This indicates
            //that the file download has completed..

            refreshList();
        }
    }
}


I think you are implementing a thread to download the video. But I suggest implementing the same with AsyncTask (also known as painless threading).

If you use AsyncTask then you need to take care of thread management. For your case, implement the AsyncTask as below.

Do all the downloading tasks inside the doInBackground() method of AsyncTask and implement alert/notification or whatever you want to show inside the onPostExecute() method of AsyncTask.


You should use AsyncTask for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜