كيفية استخدام Asynctask لإظهار ProgressDialog أثناء عمل الخلفية في Android؟ [مكرر

StackOverflow https://stackoverflow.com/questions/3893626

سؤال

تكرار ممكن:
تحديث مربع حوار التقدم في النشاط من Asynctask

أقوم بتطوير أول تطبيق Android الخاص بي وأحتاج إلى عرض ProgressDialog بينما تحدث مهمة خلفية ، في هذه الحالة مجرد مكالمة HTTP على الخادم. لقد قمت قليلاً من الدراسة حول هذا الأمر ، وقد قمت أيضًا بفحص المواضيع الأخرى المتعلقة بهذا الموضوع.

http://developer.android.com/reference/android/os/asynctask.html

يعرض Android ProgressDialog حتى ينتهي واجهة المستخدم النشاط

Android SplashScreen

http://android-developers.blogspot.com/2009/05/painless-throading.html

من بين أمور أخرى.

أكثر من أن أكتب القليل من التعليمات البرمجية:

1) في نشاطي ، أعلن أن متغيرًا من نوع ProgressDialog

public class LoginActivity extends Activity {

    public static final String TAG = "LoginActivity";

    protected ProgressDialog progressDialog; 
...

2) لقد كتبت أيضًا فئة داخلية لتوسيع Asynctask كما هو مطلوب ، هنا في DoInbackground هو المكان الذي أسميه طريقة ثابتة تقوم بالفعل بطلب Post HTTP إلى الخادم ، في جانب الخادم ، قمت بحظر استجابة الخادم 20S للتحقق من صحة مربع الحوار التقدم.

class EfetuaLogin extends AsyncTask<Object, Void, String> {

        private final static String TAG = "LoginActivity.EfetuaLogin";

        @Override
        protected void onPreExecute()
        {
            Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
        }


        @SuppressWarnings("unchecked")
        @Override
        protected String doInBackground(Object... parametros) {
            Log.d(TAG, "Executando doInBackground de EfetuaLogin");
            Object[] params = parametros;
            HttpClient httpClient = (HttpClient) params[0];
            List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
            String result = null;
            try{
            result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
            }catch (IOException e) {
                Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                e.printStackTrace();
                return result;
            }
            return result;
        }

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

    } 

3) عندما يتم الضغط على الزر i من بناء progressDialog ANC ، اتصل بـ Asynctask التي قمت بإنشائها:

    OnClickListener loginListener = new OnClickListener() {
      public void onClick(View v) {


//next line should start progress dialog in main thread ?????
   progressDialog = ProgressDialog.show(LoginActivity.this, "Login in", "Wait a moment please", true, false);

   //next couple of lines should do an ascyn call to server
   EfetuaLogin efetuaLogin = new EfetuaLogin();
   efetuaLogin.execute(params);
   try {
    //recover the server response and sets time out to be 25seconds
    sResposta = efetuaLogin.get(25, TimeUnit.SECONDS);

حسنًا ، هذا هو الحال ، أعتقد أن هذا كان من المفترض أن يُظهر حوار تقدم في حين أن Asynctask سوف يستفسر عن الخادم في الخلفية ، لكن ما أحصل عليه هو عدم وجود شريط تقدم حتى وصول الخادم ومن خلال جزء صغير من الوقت (أقل من ثانية واحدة ) يظهر التقدم والنشاط التالي يسمى.

كما ذكرت ، قمت بإعادة فحص هذا الرمز وببساطة لا أجد المكان الذي أخطأته. أي اقتراحات؟

شكرا لكم مقدما.

مرحبًا ، كما اقترح تشارلي شين (؟؟؟) في الإجابة الأولى لهذا الموضوع ، لقد حاولت تغيير القليل من الكود الخاص بي والآن أصبح مثل (لسوء الحظ لا يعمل كما هو متوقع حتى الآن):

OnClickListener loginListener = new OnClickListener() {
        public void onClick(View v) {
                //async call????????
        new EfetuaLogin().execute(params);
...

ومن خلال كل العمل للتعامل مع الاستجابة في Asynctask:

class EfetuaLogin extends AsyncTask<Object, Void, String> {

        private final static String TAG = "LoginActivity.EfetuaLogin";

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
            //inicia diálogo de progresso, mostranto processamento com servidor.
            progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
        }


        @SuppressWarnings("unchecked")
        @Override
        protected String doInBackground(Object... parametros) {
            Log.d(TAG, "Executando doInBackground de EfetuaLogin");
            Object[] params = parametros;
            HttpClient httpClient = (HttpClient) params[0];
            List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
            String result = null;
            try{
            result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
            }catch (IOException e) {
                Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                e.printStackTrace();
                return result;
            }
            return result;
        }

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

            if (result == null || result.equals("")) {
                progressDialog.dismiss();
                Alerta
                        .popupAlertaComBotaoOK(
                                "Dados incorretos",
                                "Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet.",
                                LoginActivity.this);
                return;
            }

            Log.d(TAG, "Login passou persistindo info de login local no device");
            ContentValues contentValues = new ContentValues();
            contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
            LoginDB loginDB = new LoginDB();
            loginDB.addLogin(LoginActivity.this, contentValues);
            Log.d(TAG, "Persistiu info de login no device, redirecionando para menu principal do Anototudo");
            Log.d(TAG, "O retorno da chamada foi ==>> " + result);
            // tudo ok chama menu principal
            Log.d(TAG, "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo.");

            String actionName = "br.com.anototudo.intent.action.MainMenuView";
            Intent intent = new Intent(actionName);
            LoginActivity.this.startActivity(intent);
            progressDialog.dismiss();
        }

    } 

أكمل OnClickListener:

OnClickListener loginListener = new OnClickListener() {
    public void onClick(View v) {
        Log.d(TAG, "Usuario logado, chamando menu principal");
        TextView tLogin = (TextView) findViewById(R.id.loginText);
        TextView tSenha = (TextView) findViewById(R.id.senhaText);
        String sLogin = tLogin.getText().toString();
        String sSenha = tSenha.getText().toString();

        if (sLogin.equals("") | sSenha.equals("")) {
            Alerta.popupAlertaComBotaoOK("Campos Obrigatórios",
                    "Os campos Login e Senha são obrigatórios para autenticação do Anototudo.", LoginActivity.this);
            return;
        } else {
            Pattern regEx = Pattern.compile(".+@.+\\.[a-z]+");
            Matcher matcher = regEx.matcher(sLogin);
            if (!matcher.matches()) {
                Alerta.popupAlertaComBotaoOK("Formato e-mail inválido", "O formato do campo e-mail está inválido",
                        LoginActivity.this);
                return;
            }
        }


        List<NameValuePair> listaParametros = new ArrayList<NameValuePair>();
        listaParametros.add(new BasicNameValuePair("login", sLogin));
        listaParametros.add(new BasicNameValuePair("senha", sSenha));

        Log.d(TAG, "valores recuperados dos campos de login e senha: " + sLogin + " | " + sSenha);

        // Reutiliza cliente HttpClient disponibilizado pela Aplicação.
        AnototudoApp atapp = (AnototudoApp) LoginActivity.this.getApplication();
        HttpClient httpClient = atapp.getHttpClient();

        //prepara lista de parametros para fazer chamada asíncrona no servidor para autenticar.
        Object[] params = new Object[2];
        params[0] = httpClient;
        params[1] = listaParametros;

        //faz chamada assincrona
        new EfetuaLogin().execute(params);
    }
};
هل كانت مفيدة؟

المحلول

ضعك ProgressDialog في onPreExecute, ، نموذج الرمز أدناه:

private ProgressDialog pdia;

@Override
protected void onPreExecute(){ 
   super.onPreExecute();
        pdia = new ProgressDialog(yourContext);
        pdia.setMessage("Loading...");
        pdia.show();    
}

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

وفي الخاص بك onClickListener, ، فقط ضع هذا الخط في الداخل:

new EfetuaLogin().execute(null, null , null);

نصائح أخرى

الحل النهائي الذي نجح هو أخذ جميع الكود من OnClickListener إلى doInBackground طريقة من AsyncTask تطبيق. الآن الرمز مثل:

OnClickListener:

OnClickListener loginListener = new OnClickListener() {
        public void onClick(View v) {
            /* Translation note: Original text: "Executando OnClickListener" */
            Log.d(TAG, "OnClickListener has been called");

            /* Translation note: Original text: "faz chamada assincrona" */
            // Make an asynchronous call
            new EfetuaLogin().execute();
        }
    }; 

كل العمل يحدث في EfetuaLogin AsyncTask تطبيق:

class EfetuaLogin extends AsyncTask<Object, Void, String> {

        private final static String TAG = "LoginActivity.EfetuaLogin";

        protected ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
            //inicia diálogo de progresso, mostranto processamento com servidor.
            progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
        }


        @SuppressWarnings("unchecked")
        @Override
        /* Translation note: Original text: "Object... parametros"
        protected String doInBackground(Object... parameters) {
            /* Translation note: Original text: "Executando doInBackground de EfetuaLogin" */
            Log.d(TAG, "Executing doInBackground of EfetuaLogin");
            TextView tLogin = (TextView) findViewById(R.id.loginText);
            TextView tSenha = (TextView) findViewById(R.id.senhaText);
            String sLogin = tLogin.getText().toString();
            String sSenha = tSenha.getText().toString();

            if (sLogin.equals("") | sSenha.equals("")) {
                /*
                  Translation notes:
                  1) "Campos Obrigatórios" -> "Required fields"
                  2) "Os campos Login e Senha são obrigatórios para autenticação do Anototudo." -> "Login and Password fields are required for Anototudo authentication."
                Alerta.popupAlertaComBotaoOK("Required fields", "Login and Password fields are required for Anototudo authentication.", LoginActivity.this);
                progressDialog.dismiss();
                return null;
            } else {
                Pattern regEx = Pattern.compile(".+@.+\\.[a-z]+");
                Matcher matcher = regEx.matcher(sLogin);
                if (!matcher.matches()) {
                    /*
                      Translation notes:
                      1) "Formato e-mail inválido" -> "Invalid email format"
                      2) "O formato do campo e-mail está inválido" -> "The email field has an invalid format"
                    */
                    Alerta.popupAlertaComBotaoOK("Invalid email format", "The email field has an invalid format",
                            LoginActivity.this);
                    progressDialog.dismiss();
                    return null;
                }
            }


            List<NameValuePair> listaParametros = new ArrayList<NameValuePair>();
            listaParametros.add(new BasicNameValuePair("login", sLogin));
            listaParametros.add(new BasicNameValuePair("senha", sSenha));
            /* Translation note: Original text: "valores recuperados dos campos de login e senha: " */
            Log.d(TAG, "values retrieved from login and password fields:" + sLogin + " | " + sSenha);

            /* Translation note: Original text: "Reutiliza cliente HttpClient disponibilizado pela Aplicação." */
            // Reuses `HttpClient` made available by the Application.
            AnototudoApp atapp = (AnototudoApp) LoginActivity.this.getApplication();
            HttpClient httpClient = atapp.getHttpClient();

            String result = null;
            try {
                result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
            } catch (IOException e) {
                Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                e.printStackTrace();
                return result;
            }
            return result;
        }

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

            if (result == null || result.equals("")) {
                progressDialog.dismiss();
                /*
                  Translation notes:
                  1) "Dados incorretos" -> "Incorrect data"
                  2) "Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet." -> "The reported data was not found in the System! Please report again or sign up on the internet first."
                */
                Alerta.popupAlertaComBotaoOK(
                                "Incorrect data",
                                "The reported data was not found in the System! Please report again or sign up on the internet first.",
                                LoginActivity.this);
                return;
            }
            /* Translation note: Original text: "Login passou persistindo info de login local no device" */
            Log.d(TAG, "Login passed persisting local login info on device");
            ContentValues contentValues = new ContentValues();
            contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
            LoginDB loginDB = new LoginDB();
            loginDB.addLogin(LoginActivity.this, contentValues);
            /* Translation note: Original text: "Persistiu info de login no device, redirecionando para menu principal do Anototudo" */
            Log.d(TAG, "Persisting login info on device, redirecting to Anototudo main menu");
            /* Translation note: Original text: "O retorno da chamada foi ==>> " */
            Log.d(TAG, "The callback was ==>>" + result);
            /* Translation note: Original text: "tudo ok chama menu principal" */
            // everything ok call main menu
            /* Translation note: Original text: "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo." */
            Log.d(TAG, "Device has been correctly authenticated by calling the main menu screen of Annotate.");

            String actionName = "br.com.anototudo.intent.action.MainMenuView";
            Intent intent = new Intent(actionName);
            LoginActivity.this.startActivity(intent);
            progressDialog.dismiss();
        }

    }

الآن يعمل كما هو متوقع ، لكن يجب أن أقول إنني مرتبك بعض الشيء AsyncTask تقول الوثائق أنه يمكنك استخدام التنفيذ لتمرير المعلمات إلى مهمتك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top