0

Sorry for my English. I want to do if the loading AsyncTask is continued over a certain time (in my case, more than 1 second) everything is canceled and a message is displayed to the user "Please check the Internet", I have a message displayed, but ProgressDialog continues to run.

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

I'm doing all right? I need another off ProgressDialog, it is right?

UDP:

Ideally, that's what I want to do: in app click button "avtorization" is showed animation loading(AsyncTask work) if loading more 5 second i display message "Please check the Internet" else i enter my profile. Now i have: in click button "avtorization" if i have internet 5 second nothing showed, showed fast loading animation and enter profile, if i have not internet and click "avtorization" nothing showed 5 secont then display "Please check the Internet"

Its my code now:

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();
            }
jikazali
  • 99
  • 2
  • 9

1 Answers1

1

You can get result from AsyncTask with timeout, look at method get(long timeout, TimeUnit unit)

This method blocks UI thread, so for this case better not to call get() method, but to create TimerTask, which will cancel AsyncTask after 5 seconds if it is still working.

also look: ProgressDialog not shown when AsyncTask.get() called

Community
  • 1
  • 1
Max77
  • 1,466
  • 13
  • 19
  • i use this, in my code its somthing like this `sendAvtoriz.execute( new String[]{login.getText().toString(), password.getText().toString()}).get(1, TimeUnit.SECONDS);` but if i set `get(5, TimeUnit.SECONDS);` 5 second i cant see ProgressDialog animation then its showed – jikazali Jun 28 '15 at 17:58
  • I cant understand, do you mean that after 5 seconds animation of ProgressDialog freezes? – Max77 Jun 28 '15 at 18:09
  • Ideally, that's what I want to do: in app click button "avtorization" is showed animation loading(AsyncTask work) if loading more 5 second i display message "Please check the Internet" else i enter my profile. Now i have: in click button "avtorization" if i have internet 5 second nothing showed, showed fast loading animation and enter profile, if i have not internet and click "avtorization" nothing showed 5 secont then display "Please check the Internet" – jikazali Jun 28 '15 at 18:17
  • First, if TimeOutException thrown in doInBackground() method, pDialog.dismiss() will not be called, so create pDialog outside of AsyncTask to access it in catch statement – Max77 Jun 28 '15 at 18:25
  • please look my code, i update my qestion. I edit what you say but its not work – jikazali Jun 28 '15 at 18:37