对话框不显示从另一个类

时间:2012-11-14 12:28:25

标签: java android json progressdialog

  

可能重复:
  ProgressDialog not showing up in activity

在我的主要班级,我打电话     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();
        }

    }

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

2 个答案:

答案 0 :(得分:0)

您可以使用AsyncTask在后​​台进行网络投放:

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

答案 1 :(得分:0)

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