0

可能重复:
ProgressDialog 未显示在活动中

在我的主类中,我调用 new AutoUpdate().checkForUpdate(this);

这是动作

public void checkForUpdate(Context context) {
        // check when last checked. unless overridden check once a day.

        // get current version
        int resID = context.getResources().getIdentifier("current_version",
                "string", context.getPackageName());
        currentVersion = context.getResources().getString(resID);

        // check for connection

        // now compare versions
        if (currentVersion.equals(serverVersion) == false) {
            ProgressDialog pDialog = new ProgressDialog(context);
            pDialog.setMessage("DO NOT ROTATE YOUR DEVICE. /nI am upgrading your version, press OK then INSTALL in a minute. Please wait....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            try {

                URL url = new URL(prefs.getString("server_address", null)
                        + "/updates/eduDroid.apk");
                HttpURLConnection c = (HttpURLConnection) url.openConnection();

                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                File file = new File(PATH);
                file.mkdirs();
                File outputFile = new File(file, "eduDroid.apk");
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory()
                        + "/download/"
                        + "file.apk")),
                        "application/vnd.android.package-archive");
                context.startActivity(intent);

            } catch (IOException e) {
                Toast.makeText(context, "Update error!", Toast.LENGTH_LONG)
                        .show();
            }
            pDialog.dismiss();
        }

    }

问题是下载更新版本需要很长时间。在那段时间里,用户留下一个空白屏幕。我想向他们展示对话框,但它只是没有显示。请帮忙

4

2 回答 2

0

您可以使用 anAsyncTask在后台执行您的网络工作:

    if (currentVersion.equals(serverVersion) == false) {
        new AsyncTask<Void, Void, Void>() {

            private ProgressDialog pDialog;

            @Override
            protected void onPreExecute() {
                pDialog = new ProgressDialog(context);
                pDialog.setMessage("DO NOT ROTATE YOUR DEVICE. /nI am upgrading your version, press OK then INSTALL in a minute. Please wait....");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    URL url = new URL(prefs.getString("server_address",
                            null) + "/updates/eduDroid.apk");
                    HttpURLConnection c = (HttpURLConnection) url
                            .openConnection();

                    String PATH = Environment.getExternalStorageDirectory()
                            + "/download/";
                    File file = new File(PATH);
                    file.mkdirs();
                    File outputFile = new File(file, "eduDroid.apk");
                    FileOutputStream fos = new FileOutputStream(outputFile);

                    InputStream is = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                    }
                    fos.close();
                    is.close();

                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory()
                            + "/download/"
                            + "file.apk")),
                            "application/vnd.android.package-archive");
                    context.startActivity(intent);

                } catch (IOException e) {
                    Toast.makeText(context, "Update error!",
                            Toast.LENGTH_LONG).show();
                }
            }

            @Override
            protected void onPostExecute(Void result) {
                pDialog.dismiss();
            }
        }.execute();
    }
于 2012-11-14T12:35:06.167 回答
0

在将 Context 传递给对话框时,尝试使用处理程序或 Runnable 来显示进度对话框和 (ActivityName.this) 而不是 (this)。

于 2012-11-14T12:53:44.303 回答