使用AsyncTask的类平行operationand显示进度条 [英] using AsyncTask class for parallel operationand displaying a progress bar

查看:245
本文介绍了使用AsyncTask的类平行operationand显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用显示任务异步类simulatneously并行操作的进度条,我想要从另一个类的函数,需要一些时间来返回字符串数组的字符串数组。

的问题是,当我放置在这样做backgroung AsyncTask的类的功能的函数呼叫,它在这样的背景提供了一个错误,并提供作为倾斜更改UI做背景..消息

因此​​,我把函数调用后执行的AsyncTask类的方法。它不列入给出一个错误,但进度条后,已经达到了100%,然后屏幕变黑,并需要一定的时间来启动新的活动。

我怎么能显示进度条,使函数调用simultaneously.??plz帮助下,米遇险

这里是code

 包com.integrated.mpr;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类陆侃扩展活动实现OnClickListener {    静态的String [] =显示新的String [Choose.n]
    按钮bprogress;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.progress);
        bprogress =(按钮)findViewById(R.id.bProgress);
        bprogress.setOnClickListener(本);
    }    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根        开关(v.getId()){
        案例R.id.bProgress:
            字符串x =ABC;
            新loadSomeStuff()执行(x);
            打破;
        }
    }    公共类loadSomeStuff扩展的AsyncTask<字符串,整数,字符串> {        ProgressDialog对话框;
        在preExecute保护无效(){
            对话框=新ProgressDialog(Progess.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.show();
        }
        @覆盖
        保护字符串doInBackground(字符串...为arg0){
            // TODO自动生成方法存根            的for(int i = 0; I< 40;我++){
                publishProgress(5);                尝试{
                    视频下载(1000);
                }赶上(InterruptedException的E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
            }            dialog.dismiss();
            字符串Y =ABC;
            返回是;
        }        保护无效onProgressUpdate(整数...进度){            dialog.incrementProgressBy(进展[0]);
        }        保护无效onPostExecute(字符串结果){
            显示=新逻辑()FinalData的()。
            意图openList =新意图(com.integrated.mpr.SENSITIVELIST);
            startActivity(openList);
        }
    }
}


解决方案

您不能驳回doInBackground()对话框 - 甚至解雇一个对话框,需要UI任务。移动dialog.dismiss()到的AsyncTask的onPostExecute()。

I am displaying a progress bar using Async task class and simulatneously in parallel operation , i want to retrieve a string array from a function of another class that takes some time to return the string array.

The problem is that when i place the function call in doing backgroung function of AsyncTask class , it gives an error in Doing Background and gives the message as cant change the UI in doing Background ..

Therefore , i placed the function call in post Execute method of Asynctask class . It doesnot give an error but after the progress bar has reached 100% , then the screen goes black and takes some time to start the new activity.

How can i display the progress bar and make the function call simultaneously.??plz help , m in distress

here is the code

package com.integrated.mpr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Progess extends Activity implements OnClickListener{



    static String[] display = new String[Choose.n];
    Button bprogress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress);  
        bprogress = (Button) findViewById(R.id.bProgress);
        bprogress.setOnClickListener(this); 
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){
        case R.id.bProgress:        
            String x ="abc";    
            new loadSomeStuff().execute(x);     
            break;
        }
    }

    public class loadSomeStuff extends AsyncTask<String , Integer , String>{

        ProgressDialog dialog;
        protected void onPreExecute(){
            dialog = new ProgressDialog(Progess.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.show();
        }
        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            for(int i = 0 ;i<40;i++){
                publishProgress(5);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            dialog.dismiss();
            String y ="abc";
            return y;
        }

        protected void onProgressUpdate(Integer...progress){

            dialog.incrementProgressBy(progress[0]);
        }

        protected void onPostExecute(String result){
            display = new Logic().finaldata();
            Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
            startActivity(openList);    
        }
    }
}

解决方案

You can't dismiss the dialog in doInBackground() - even dismissing a dialog needs the UI task. Move dialog.dismiss() to onPostExecute() of the AsyncTask.

这篇关于使用AsyncTask的类平行operationand显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆