0

大家好,我有一个应用程序,它通过单击按钮将一些文件存储到 sdcard,但我想显示它正在下载或下载的指示。

如何实现这一点我可以使用进度条。

这是我的按钮代码

case R.id.dd:
File sdcard = Environment.getExternalStorageDirectory();
            File dir = new File (sdcard.getAbsolutePath() + "/varun");
            dir.mkdirs();
            File file = new File(dir, "" +var+ ".mp3");

        //  File file = new File(Environment.getExternalStorageDirectory(), "" +ver+ ".mp3");
            FileOutputStream fos;

            try {
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                // handle exception
            } catch (IOException e) {
                // handle exception
            }
4

2 回答 2

2

使用AsyncTask你可以实现这个....

http://developer.android.com/reference/android/os/AsyncTask.html

http://www.vogella.de/articles/AndroidPerformance/article.html

于 2012-02-29T07:58:44.640 回答
0

你可以使用异步任务或线程类..使用线程你可以使用下面的代码

ProgressBar prg;
    private void start_thread(final int offer_id)
    {
     prg=ProgressDialog.show(this, null, "Getting data...",false,false);
         new Thread(new Runnable() 
         { public void run() 
         { 
            try {
                start_prc();
                mHandlerSuccess.post(mUpdateSuccess);
            } catch (Exception e) {

                mHandlerFail.post(mUpdateFail);
            }   
        }
      } ).start();
    }
    final Handler mHandlerSuccess= new Handler();

    final Runnable mUpdateSuccess = new Runnable() {
        public void run() {
            prg.hide();
            Toast.makeText(Add_remove_btnsActivity.this, "finished", Toast.LENGTH_LONG).show();

        }
    };
    final Handler mHandlerFail= new Handler();

    final Runnable mUpdateFail = new Runnable() {
        public void run() {
            prg.hide();
            Toast.makeText(Add_remove_btnsActivity.this, "failed", Toast.LENGTH_LONG).show();

        }
    };
    private void start_prc()
    {
        File dir = new File (sdcard.getAbsolutePath() + "/varun");
        dir.mkdirs();
        File file = new File(dir, "" +var+ ".mp3");

    //  File file = new File(Environment.getExternalStorageDirectory(), "" +ver+ ".mp3");
        FileOutputStream fos;

        try {
            fos = new FileOutputStream(file);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // handle exception
            mHandlerFail.post(mUpdateFail);
        } catch (IOException e) {
            // handle exception
            mHandlerFail.post(mUpdateFail);
        }

    }
于 2012-02-29T08:02:58.323 回答