Android:外部异步类中的ProgressDialog(sth阻止异步线程)

时间:2014-12-05 14:26:59

标签: android asynchronous android-asynctask external progressdialog

我有主要活动:

public class ChooseWriteSentenceActivity extends ActionBarActivity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        String userName = "Zdzisiu";
        String password = "Ziemniak";
        MainServie service = new MainService(this);
        boolean isExsist = service.findUser(String userName, String password);
        //more code...
    }   
}

在我的应用服务中使用存储库和jsonconsumers但是对于更简单的代码我正在跳过它们。

public class MyService{
    private Context context;
    public MyService(Context context){
        this.context = context
    }
    public boolean findUser(String userName, String password){
        String resultS = null;
        try{
            resultS = new QueryExecutorFindUser(context).execute(userName,password).get();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        boolean realRes = jsonConsumer(resultS).getFindUser();
        return realRes;
    }
}

public class QueryExecutorFindUser extends AsyncTask<String,Void,String> {


protected final String connectionUrl = "http://myWebService:44302/Service.svc/";

    protected ProgressDialog progressDialog; 
    protected Context curContext;

    public QueryExecutor(Context context){
        curContext = context;
    }

    @Override  
    protected void onPreExecute()  
    {  
        super.onPreExecute();
        progressDialog = ProgressDialog.show(curContext,"Loading...",  
                "Loading application View, please wait...", false, false);  
    }

    @Override  
    protected void onPostExecute(String result)  
    {  
        super.onPostExecute(result);
        progressDialog.dismiss();   
    } 

    protected String doInBackground(String... args){
        String result = null;
        String url = connectionUrl + args[0] + "/" + args[1];
        HttpResponse response = null;
        HttpClient httpclient =  this.getNewHttpClient();
        HttpGet get = new HttpGet(url);
        get.setHeader("Accept", "application/json"); 
        get.setHeader("Content-type", "application/json"); 
        try{
            response = httpclient.execute(get);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                if(response != null){
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    out.close();
                    result = out.toString();
                }
            } else{
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch(Exception ex){
            ex.getMessage();
        } finally{
            if(response != null){
                try{
                    response.getEntity().getContent().close();
                } catch(Exception ex){
                }
            }
        }

        return result;
    }
}

显示进度对话框,但只有在来自QueryExecutor的ChooseWriteSentenceActivity中的onCreatre中的所有代码(包括来自QueryExecutor的doInBacground(...))完成后(因此它几乎同时消失)。看起来好像在等待使用QueryExecutorFindUser.doInBackground()的线程并且它像同步(?)一样运行,我认为因为当我调试代码onPreExecute()正确运行(并在doInBackground(...)之前启动)和progressDialog .isShowing()== true(但不在屏幕上:()。

如果我从QueryExecutorFindUser中删除扩展的AsyncTask,并在main活动中使用此扩展名创建私有类(并在thisPrivateClass.doInBackground(...)中运行onCreated()的所有代码,包括service.findUser()),它可以正常工作。

我更喜欢将progressDialog放在一个地方没有在所有主要活动中(在实践中我使用QueryExecutor进行所有查询,而不仅仅是findUser)但我不知道我做错了什么。我花了一整天没有结果:(

1 个答案:

答案 0 :(得分:1)

对话框与Activity绑定,最终必须由一个人托管。因此,在创建应用程序的活动之前,对话框将不会显示。