在我的android应用程序上,我有自己的BaseAdapter,在此我有一个带有以下代码的clickbutton侦听器:

btnShareFacebook.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            videoMoreLayout.setVisibility(View.GONE);
            final String id = videoIDs[position];
            videoActions = new VideoActions();
            hfu = new HttpFileUpload(videoPathURL[position], token);

            final ProgressDialog pDialog = new ProgressDialog(activity);
            pDialog.setMessage("Preparing video to share...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);

            if (!isOnline()) {
                Toast.makeText(activity,
                        activity.getString(R.string.noInternetConnection),
                        Toast.LENGTH_SHORT).show();
            } else {

                try {
                    String arr[] = videoPathURL[position].split("/upload/videos/");
                    final String finalVideoName = (arr[arr.length-1]);

                    File DoutFile = new File(
                            Environment.getExternalStorageDirectory() +
                            File.separator + "vidytape_shared" + File.separator + finalVideoName);

                    if(!DoutFile.exists()){

                        pDialog.show();

                        String downloadResult = videoActions.downloadToShare(token, activity, id, videoPathURL[position], finalVideoName);
                        if (downloadResult.equalsIgnoreCase("POST_FAILURE") || downloadResult.equalsIgnoreCase("FAILURE_102")) {
                            Toast.makeText(
                                    activity,
                                    activity.getString(R.string.someErrorOccurred),
                                    Toast.LENGTH_SHORT).show();
                        }

                        /*if(hfu.Download_File_To_Share(finalVideoName, videoPathURL[position])){
                            Toast.makeText(activity, "OK", Toast.LENGTH_LONG).show();
                            //pDialog.dismiss();
                        } else {
                            Toast.makeText(activity, "ERRO", Toast.LENGTH_LONG).show();
                        }*/


                    }

                    pDialog.dismiss();

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("video/*");
                    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(DoutFile));
                    share.putExtra(Intent.EXTRA_TEXT,
                            "" + activity.getResources().getString(R.string.app_name));
                    share.setPackage("com.facebook.katana");
                    activity.startActivity(share);

                } catch (Exception e) {
                    pDialog.dismiss();
                    Toast.makeText(
                            activity,
                            activity.getString(R.string.facebookNotInstalled),
                            Toast.LENGTH_LONG).show();
                }

            }
        }
    });


现在,在这里,您具有类VideoActions的功能downloadToShare:

public String downloadToShare(String token, Activity activity, String videoID, String videoPath, String videoName) {

    this.token      = token;
    this.activity   = activity;
    this.videoId    = videoID;
    this.videoPath  = videoPath;
    this.videoName  = videoName;

    DownloadToShareClass downloadAction = new DownloadToShareClass();

    try {
        return downloadAction.execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    return null;
} // Close download


现在是downloadToShare类:

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

    String response = null;

@Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(activity);
            pDialog.setMessage("Preparing video to share...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

    @Override
    protected String doInBackground(String... args) {

                HttpFileUpload hfu = new HttpFileUpload(videoPath, token);
                hfu.Download_File_To_Share(videoName, videoPath);

                return "SUCESSFULLY";
    }

} // Close DownloadClass


现在,download_file_to_share:

boolean Download_File_To_Share(String fileName, String urlToDownload) {

    boolean downloadResult;
    try {
        downloadResult = DownloadFileToShare(fileName, urlToDownload);
        return downloadResult;
    } catch (MalformedURLException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}


最后,DownloadFileToShare:

// Download video
boolean DownloadFileToShare(String filename, String urlToDownload)
        throws MalformedURLException, IOException {

    BufferedInputStream in = null;
    FileOutputStream fout = null;
    try {
        URL url = new URL(urlToDownload);

        in = new BufferedInputStream(url.openStream());
        fout = new FileOutputStream(
                Environment.getExternalStorageDirectory() +
                File.separator + "vidytape_shared" + File.separator + filename);

        byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
            System.out.println(count);
        }
    } finally {
        if (in != null)
            in.close();
        if (fout != null)
            fout.close();
    }
    return true;
}


正如我在BaseAdapter代码上看到的那样,我的问题是,我正在显示一个进度对话框,但只有在所有其余步骤(视频下载)之后才会显示该对话框。这里有什么问题 ?我希望在下载视频时显示progressDialog ...

提前致谢

我已经找到了解决自己问题的方法。在我的BaseAdapter中,我将点击侦听器代码更改为以下代码,现在可以正常使用:

btnShareFacebook.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            videoMoreLayout.setVisibility(View.GONE);
            hfu = new HttpFileUpload(videoPathURL[position], token);

            final ProgressDialog pDialog = new ProgressDialog(activity);
            pDialog.setMessage(activity.getResources().getString(R.string.preparingVideoToShare));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);

            if (!isOnline()) {
                Toast.makeText(activity,
                        activity.getString(R.string.noInternetConnection),
                        Toast.LENGTH_SHORT).show();
            } else {

                try {
                    String arr[] = videoPathURL[position].split("/upload/videos/");
                    final String finalVideoName = (arr[arr.length-1]);

                    final File DoutFile = new File(
                            Environment.getExternalStorageDirectory() +
                            File.separator + "vidytape_shared" + File.separator + finalVideoName);

                    if(!DoutFile.exists()){

                        pDialog.show();

                        (new Thread(new Runnable() {

                            @Override
                            public void run() {
                                final String a = hfu.Download_File_To_Share(finalVideoName, videoPathURL[position]);
                                activity.runOnUiThread(new Runnable() {
                                    public void run() {
                                        if (!a.equalsIgnoreCase("ok")){
                                            pDialog.dismiss();
                                            Toast.makeText(activity,
                                                    activity.getResources().getString(R.string.someErrorOccurred),
                                                    Toast.LENGTH_LONG).show();
                                        } else {
                                            pDialog.dismiss();
                                            Intent share = new Intent(Intent.ACTION_SEND);
                                            share.setType("video/*");
                                            share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(DoutFile));
                                            share.putExtra(Intent.EXTRA_TEXT,
                                                    "" + activity.getResources().getString(R.string.app_name));
                                            share.setPackage("com.facebook.katana");
                                            activity.startActivity(share);
                                        }
                                    }
                                });
                            }
                        })).start();

                    } else {
                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("video/*");
                        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(DoutFile));
                        share.putExtra(Intent.EXTRA_TEXT,
                                "" + activity.getResources().getString(R.string.app_name));
                        share.setPackage("com.facebook.katana");
                        activity.startActivity(share);
                    }

                } catch (Exception e) {
                    if(pDialog.isShowing()){
                        pDialog.dismiss();
                    }
                    e.printStackTrace();
                    Toast.makeText(
                            activity,
                            activity.getString(R.string.facebookNotInstalled),
                            Toast.LENGTH_LONG).show();
                }

            }
        }
    });


我希望它也可以帮助某人。

最佳答案

您是否可以在扩展AsyncTask的DownloadToShareClass类的onPreExecute中添加代码以显示进度栏?

请参考http://developer.android.com/reference/android/os/AsyncTask.html

从上面的链接-
onPreExecute(),在执行任务之前在UI线程上调用。此步骤通常用于设置任务,例如,通过在用户界面中显示进度栏。

class DownloadToShareClass extends AsyncTask<String, String, String> {
private ProgressDialog mdialog;
public DownloadToShareClass(yourActivity activity) {
    mdialog = new ProgressDialog(activity);
}
@Override
protected void onPreExecute() {
    mdialog.show();
}
@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void onPostExecute(Void result) {
    if (mdialog.isShowing()) {
        mdialog.dismiss();
    }
}


}

关于java - 来自Baseadapter的Android Java progressdialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25127123/

10-12 04:32