Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对话框未从其他类显示_Java_Android_Json_Progressdialog - Fatal编程技术网

Java 对话框未从其他类显示

Java 对话框未从其他类显示,java,android,json,progressdialog,Java,Android,Json,Progressdialog,可能重复: 我在主课上打电话 新建自动更新()。checkForUpdate(此) 这就是行动 public void checkForUpdate(Context context) { // check when last checked. unless overridden check once a day. // get current version int resID = context.getResources().getIdent

可能重复:

我在主课上打电话 新建自动更新()。checkForUpdate(此)

这就是行动

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

    }

问题是下载更新版本需要很长时间。在这段时间内,用户将被留下一个空白屏幕。我想给他们看这个对话框,但它就是不显示。请提供帮助

您可以使用
异步任务
在后台处理网络事务:

    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();
    }
if(currentVersion.equals(serverVersion)==false){
新建异步任务(){
私人对话;
@凌驾
受保护的void onPreExecute(){
pDialog=新建进度对话框(上下文);
pDialog.setMessage(“请勿旋转您的设备。/n我正在升级您的版本,请按“确定”,然后在一分钟内安装。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
URL URL=新URL(prefs.getString(“服务器地址”),
null)+“/updates/eduDroid.apk”);
HttpURLConnection c=(HttpURLConnection)url
.openConnection();
String PATH=Environment.getExternalStorageDirectory()
+“/下载/”;
文件=新文件(路径);
mkdirs()文件;
File outputFile=新文件(文件“eduDroid.apk”);
FileOutputStream fos=新的FileOutputStream(outputFile);
InputStream=c.getInputStream();
字节[]缓冲区=新字节[1024];
int len1=0;
而((len1=is.read(buffer))!=-1){
fos.写入(缓冲区,0,len1);
}
fos.close();
is.close();
意向意向=新意向(意向.行动\视图);
intent.setDataAndType(Uri.fromFile)(新文件(环境
.getExternalStorageDirectory()
+“/下载/”
+“file.apk”),
“application/vnd.android.package归档文件”);
背景。开始触觉(意图);
}捕获(IOE异常){
Toast.makeText(上下文,“更新错误!”,
Toast.LENGTH_LONG).show();
}
}
@凌驾
受保护的void onPostExecute(void结果){
pDialog.disclose();
}
}.execute();
}

在向对话框传递上下文时,尝试使用handler或Runnable显示进度对话框和(ActivityName.this)而不是(this)。

wtf是否获取剩余值?为什么不使用getString(R.string.current_版本)?使用asynctask或activity.showDialog()。您正试图在ui线程外显示一个对话框,它是forbidden@njzk2谢谢你指出这一点!这是一个名称的变化,但现在改变了。谢谢,这似乎已经奏效。