onDownload完成通知 - inapp购买的Andr​​oid [英] onDownload complete notification - inapp purchase in Android

查看:95
本文介绍了onDownload完成通知 - inapp购买的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我下载过的应用程序购买的视频从城市飞艇

当我点击我的应用程序中的特定按钮下载得到启动。在这样的情况下,在下载充分完成后欲使按钮成为不可见来获得一个通知或报警。我怎么能知道该视频已完全下载?

有没有一块code我可以写在下载完成后在Android中得到通知?

解决方案

 私人无效startDownload(){
        字符串URL =这里写您的网址;
        新DownloadFileAsync()执行(URL);

    }

    @覆盖
    受保护的对话框onCreateDialog(INT ID){
        开关(ID){
        案例DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog =新ProgressDialog(本);

            // mProgressDialog.setIcon(R.drawable.icon);
            mProgressDialog.setIcon(R.drawable.tab_icon_pitchforkfm);
            mProgressDialog.setTitle(下载文件...
                    + GlobalVariable.Getstrpath()的toString())。

            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(真正的);
            mProgressDialog.show();
            返回mProgressDialog;
        默认:
            返回null;
        }
    }

    类DownloadFileAsync扩展的AsyncTask<字符串,字符串,字符串> {

        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            的ShowDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        / **
         *这是用于下载文件的背景
         * 处理。
         * /
        @覆盖
        保护字符串doInBackground(字符串...为arg0){
            // TODO自动生成方法存根。
            // mp3load();
            INT LEN1 = 0;
            诠释计数;
            尝试 {
                网址URL =新的URL(GlobalVariable.Getstr());
                HttpURLConnection的C =(HttpURLConnection类)url.openConnection();
                c.setRequestMethod(GET);
                c.setDoOutput(真正的);
                c.connect();
                INT lenghtOfFile = c.getContentLength();
                Log.d(ANDRO_ASYNC的Lenght文件:+ lenghtOfFile);
                字符串PATH = Environment.getExternalStorageDirectory()+
                              /下载/;
                Log.v(LOG_TAG,路径:+路径);
                档案文件=新的文件(路径);
                file.mkdirs();

                //字符串文件名=workTest.mp3;
                字符串文件名=请将test.mp3;

                文件OUTPUTFILE =新的文件(文件,文件名);

                FileOutputStream中FOS =新的FileOutputStream(OUTPUTFILE);

                InputStream的是= c.getInputStream();

                byte []的缓冲区=新的字节[1024];
                总长= 0;

                而((计数= is.​​read(缓冲液))!=  -  1){
                    共有+ =计数;
                    publishProgress(+(int)的((总* 100)/ lenghtOfFile));
                    // fos.write(缓冲液,0,LEN1);
                    fos.write(缓冲,0,计数);
                }
                fos.close();
                is.close();

            }赶上(IOException异常E){
                Log.d(LOG_TAG,错误:+ E);
            }
            返回null;
        }

        保护无效onProgressUpdate(字符串...进度){
            Log.d(ANDRO_ASYNC,进步[0]);

            mProgressDialog.setProgress(的Integer.parseInt(进展[0]));
        }

        保护无效onPostExecute(字符串使用){
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            // refreshList();
            Toast.makeText(
                FindFilesByType.this,
                +文件名+完成了。中下载,
                Toast.LENGTH_LONG).show();

            //把你的code在这里。也就是说,按钮显示/隐藏code等这表明
            //该文件下载完成..

            refreshList();
        }
    }
}
 

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 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();
        }
    }
}

这篇关于onDownload完成通知 - inapp购买的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆