Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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
android进程对话框的同步_Android_Progressdialog - Fatal编程技术网

android进程对话框的同步

android进程对话框的同步,android,progressdialog,Android,Progressdialog,我有两个活动叫做Client1.java和Client2.java Client1创建与远程主机的tcp/ip连接以运行命令。然后服务器将输出发送回Client1。输出将显示在Client2活动中。由于需要一段时间才能将输出从远程服务器发送回Client1活动,因此我使用progreesDialog来显示一些进度,直到输出完全从服务器发送回Client1活动为止 我需要一些同步,即当输出将完全发送回Client1时,activity progressDialog将完成,Client2将开始显示从

我有两个活动叫做Client1.java和Client2.java

Client1创建与远程主机的tcp/ip连接以运行命令。然后服务器将输出发送回Client1。输出将显示在Client2活动中。由于需要一段时间才能将输出从远程服务器发送回Client1活动,因此我使用progreesDialog来显示一些进度,直到输出完全从服务器发送回Client1活动为止

我需要一些同步,即当输出将完全发送回Client1时,activity progressDialog将完成,Client2将开始显示从远程服务器接收的输出

是实现异步线程的概念,在Android中被称为异步线程,因为如果实现了异步线程,那么就不需要担心线程管理

现在,您只需要在特定方法中编写逻辑

例如:

  • onPreExecute()-显示进度对话框的位置

  • doInBackground()-在需要编写长时间运行的任务的地方,远程调用逻辑就在这里

  • onPostExecute()-在这里,您需要编写启动Client2活动的代码,并关闭对话框

  • 是实现异步线程的概念,在Android中称为异步线程,因为如果实现了异步线程,则无需担心线程管理

    现在,您只需要在特定方法中编写逻辑

    例如:

  • onPreExecute()-显示进度对话框的位置

  • doInBackground()-在需要编写长时间运行的任务的地方,远程调用逻辑就在这里

  • onPostExecute()-在这里,您需要编写启动Client2活动的代码,并关闭对话框


  • 帕雷什是对的!!!看一个例子

    private class SessionTask extends AsyncTask<String, Integer, Integer> {
    
        ProgressDialog dialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            dialog = new ProgressDialog(TestUI.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setTitle("UploadFile");
            dialog.setMessage("Uploading file...");
            dialog.setCancelable(false);
            dialog.setProgress(0);
            dialog.show();
        }
    
        @Override
        protected Integer doInBackground(String... params) {
            .........
            } catch(MalformedURLException e) {
                Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());
    
                return 1;
            } catch(IOException e) {
                Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
            return 2;
            }
            return 0;
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            dialog.setMax(values[1]);
            dialog.setProgress(values[0]);
    
        }
    
        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            dialog.dismiss();
            switch (result) {
            case 0:
                Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
    
                new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
                break;
            case 1:
                Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
                break;
            case 2:
                Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
    
                break;
            }
        }
    }
    
    私有类SessionTask扩展了异步任务{
    进程对话;
    @凌驾
    受保护的void onPreExecute(){
    super.onPreExecute();
    dialog=newprogressdialog(TestUI.this);
    setProgressStyle(ProgressDialog.STYLE_微调器);
    setTitle(“上传文件”);
    setMessage(“上传文件…”);
    对话框。可设置可取消(false);
    对话框。设置进度(0);
    dialog.show();
    }
    @凌驾
    受保护的整数doInBackground(字符串…参数){
    .........
    }捕获(格式错误){
    Log.e(TestUI.TAG,“e:格式错误的URL!”+e.getLocalizedMessage());
    返回1;
    }捕获(IOE异常){
    Log.e(TestUI.TAG,“e:I/O错误!”+e.getLocalizedMessage());
    返回2;
    }
    返回0;
    }
    @凌驾
    受保护的void onProgressUpdate(整型…值){
    super.onProgressUpdate(值);
    setMax(值[1]);
    dialog.setProgress(值[0]);
    }
    @凌驾
    受保护的void onPostExecute(整数结果){
    super.onPostExecute(结果);
    dialog.dismise();
    开关(结果){
    案例0:
    Toast.makeText(TestUI.this,“上传完成”,Toast.LENGTH_LONG.show();
    new DownloadTask().execute(新字符串[]{TestUI.LINK_DOWN,TestUI.FILE_DOWN});
    打破
    案例1:
    Toast.makeText(TestUI.this,“E:格式错误的URL!”,Toast.LENGTH\u LONG.show();
    打破
    案例2:
    Toast.makeText(TestUI.this,“E:I/O错误!连接被解除!!!”,Toast.LENGTH\u LONG.show();
    打破
    }
    }
    }
    
    帕雷什是对的!!!看一个例子

    private class SessionTask extends AsyncTask<String, Integer, Integer> {
    
        ProgressDialog dialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            dialog = new ProgressDialog(TestUI.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setTitle("UploadFile");
            dialog.setMessage("Uploading file...");
            dialog.setCancelable(false);
            dialog.setProgress(0);
            dialog.show();
        }
    
        @Override
        protected Integer doInBackground(String... params) {
            .........
            } catch(MalformedURLException e) {
                Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());
    
                return 1;
            } catch(IOException e) {
                Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
            return 2;
            }
            return 0;
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            dialog.setMax(values[1]);
            dialog.setProgress(values[0]);
    
        }
    
        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            dialog.dismiss();
            switch (result) {
            case 0:
                Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
    
                new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
                break;
            case 1:
                Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
                break;
            case 2:
                Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
    
                break;
            }
        }
    }
    
    私有类SessionTask扩展了异步任务{
    进程对话;
    @凌驾
    受保护的void onPreExecute(){
    super.onPreExecute();
    dialog=newprogressdialog(TestUI.this);
    setProgressStyle(ProgressDialog.STYLE_微调器);
    setTitle(“上传文件”);
    setMessage(“上传文件…”);
    对话框。可设置可取消(false);
    对话框。设置进度(0);
    dialog.show();
    }
    @凌驾
    受保护的整数doInBackground(字符串…参数){
    .........
    }捕获(格式错误){
    Log.e(TestUI.TAG,“e:格式错误的URL!”+e.getLocalizedMessage());
    返回1;
    }捕获(IOE异常){
    Log.e(TestUI.TAG,“e:I/O错误!”+e.getLocalizedMessage());
    返回2;
    }
    返回0;
    }
    @凌驾
    受保护的void onProgressUpdate(整型…值){
    super.onProgressUpdate(值);
    setMax(值[1]);
    dialog.setProgress(值[0]);
    }
    @凌驾
    受保护的void onPostExecute(整数结果){
    super.onPostExecute(结果);
    dialog.dismise();
    开关(结果){
    案例0:
    Toast.makeText(TestUI.this,“上传完成”,Toast.LENGTH_LONG.show();
    new DownloadTask().execute(新字符串[]{TestUI.LINK_DOWN,TestUI.FILE_DOWN});
    打破
    案例1:
    Toast.makeText(TestUI.this,“E:格式错误的URL!”,Toast.LENGTH\u LONG.show();
    打破
    案例2:
    Toast.makeText(TestUI.this,“E:I/O错误!连接被解除!!!”,Toast.LENGTH\u LONG.show();
    打破
    }
    }
    }