如果加载超过一定时间,则关闭AsyncTask(android)

时间:2015-06-28 17:48:40

标签: java android android-asynctask

抱歉我的英文。我想要做的是,如果加载AsyncTask持续一段时间(在我的情况下,超过1秒)一切都被取消并向用户显示一条消息“请检查互联网”,我有一条消息显示,但ProgressDialog继续跑。

    SendRequestAutorization sendAvtoriz = new SendRequestAutorization ();

    //code

         try {
                                sendAvtoriz.execute(
                                        new String[]{login.getText().toString(), password.getText().toString()}).get(1, TimeUnit.SECONDS);
                            } catch(Exception e) {
                                Log.e("Avtorization", e.toString());
                                Toast.makeText(getApplicationContext(), "Please check the Internet", Toast.LENGTH_SHORT).show();
                                sendAvtoriz.cancel(true);
                            }


//code
private class SendRequestAutorization extends AsyncTask<String, String, String> {
        ProgressDialog pDialog;

 @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Avtorization.this);
            pDialog.setMessage("Authorization");
            pDialog.setCancelable(false);
            pDialog.show();
        }

protected String doInBackground(String... params) {
//code
   return null;
        }

protected void onPostExecute(String result) {
            super.onPostExecute(result);
            pDialog.dismiss();
        }

我做得好吗?我需要另一个关闭ProgressDialog,它是对的吗?

UDP:

理想情况下,这就是我想要做的事情:在应用程序点击按钮“avtorization”显示动画加载(AsyncTask工作)如果加载更多5秒我显示消息“请检查互联网”否则我输入我的个人资料。现在我有:在点击按钮“avtorization”,如果我有互联网5秒没有显示,显示快速加载动画和输入个人资料,如果我没有互联网和点击“avtorization”没有显示5 secont然后显示“请检查互联网”< / p>

现在是我的代码:

private Button logInn;
 private SendRequestAutorization sendAvtoriz;
 ProgressDialog pDialog;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p_avtorization);

 logInn = (Button) findViewById(R.id.createAccount);
 sendAvtoriz = new SendRequestAutorization();


 logInn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

  pDialog = new ProgressDialog(Avtorization.this);
                    pDialog.setMessage("Authorization");
                    pDialog.setCancelable(false);
                    pDialog.show();


try {
                        sendAvtoriz.execute(
                                new String[]{login.getText().toString(), password.getText().toString()}).get(5, TimeUnit.SECONDS);
                    } catch(Exception e) {
                        Log.e("Avtorization", e.toString());
                        Toast.makeText(getApplicationContext(), "Please check the Internet", Toast.LENGTH_SHORT).show();
                        sendAvtoriz.cancel(true);
                        pDialog.dismiss();
                    }
}

}

 //code
    private class SendRequestAutorization extends AsyncTask<String, String, String> {


    protected String doInBackground(String... params) {
    //code
       return null;
            }

    protected void onPostExecute(String result) {
                super.onPostExecute(result);
                pDialog.dismiss();
            }

1 个答案:

答案 0 :(得分:1)

您可以通过超时从AsyncTask获取结果,查看方法get(long timeout, TimeUnit unit)

此方法阻止UI线程,因此对于这种情况最好不要调用get()方法,而是创建TimerTask,如果它仍在工作,它将在5秒后取消AsyncTask。

另外看: ProgressDialog not shown when AsyncTask.get() called

相关问题