1

我正在尝试使用从用户单击按钮到发送电子邮件的不确定进度条。我打开用于发送电子邮件的线程和我认为的进度条有问题。现在它只是在两者靠近时崩溃,我不确定是否有一种聪明的方法来启用进度条(基本上我只想要在发送电子邮件时旋转圆圈的动画,以便用户有一些反馈背景中正在发生的事情)。

创建时

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.signin_page);

    verify_button = (Button) findViewById(R.id.verify_button);
    signin_button = (Button) findViewById(R.id.signin_button);

    staysignedin = (CheckBox) findViewById(R.id.staysignedinCheck);

    link4help = (TextView) findViewById(R.id.link_to_register);

    gmail = (EditText) findViewById(R.id.signin_email);
    password = (EditText) findViewById(R.id.signin_password);

    email_success = (ImageView) findViewById(R.id.email_authenticate_success);
    password_success = (ImageView) findViewById(R.id.password_authenticate_success);

    email_success.setVisibility(View.INVISIBLE);
    password_success.setVisibility(View.INVISIBLE);

    signin_button.setEnabled(false);

    verify_button.setOnClickListener(this);
    signin_button.setOnClickListener(this);
    link4help.setOnClickListener(this);

    final float scale = this.getResources().getDisplayMetrics().density;
    staysignedin.setPadding(staysignedin.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
            staysignedin.getPaddingTop(),
            staysignedin.getPaddingRight(),
            staysignedin.getPaddingBottom());

     /* Setting up handler for ProgressBar */
    //b = (ProgressBar) findViewById(R.id.progressBar_verify);
    setProgressBarIndeterminateVisibility(false);

}

点击

@Override
public void onClick(View v) {
    setProgressBarIndeterminateVisibility(true);
    //Log.e("verify clicked","hi");
    switch(v.getId()){


    case R.id.verify_button:

        Thread thread = new Thread(){
            public void run(){


                String gmailString = gmail.getText().toString();
                String passString = password.getText().toString();
                String[] recip = new String[]{gmailString};
                String body = "\nThis is a test for the amazing Dictation2Go App!   Created by --";
                    MailAccount a  = new MailAccount(gmailString,passString);
                    try {
                        isGoogleAuthenticated = a.sendEmailTo(recip, "test", body);
                    } catch (MessagingException e) {
                           Log.e("failed to connect", "mess: "+e.getMessage());
                           isGoogleAuthenticated = false;
                    }               
            }
        };
        thread.start();

        try {
            thread.join();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }


        Log.e("END RESULT",String.valueOf(isGoogleAuthenticated));

        if(isGoogleAuthenticated){
            pb.setVisibility(View.INVISIBLE);
            email_success.setVisibility(View.VISIBLE);
            password_success.setVisibility(View.VISIBLE);
            signin_button.setEnabled(true);
            password.setEnabled(false);
            gmail.setEnabled(false);

            gmail.setBackgroundResource(R.layout.bordersuccess);
            password.setBackgroundResource(R.layout.bordersuccess);
        }else{
            gmail.setText("");
            password.setText("");
        }
        setProgressBarIndeterminateVisibility(false);
        break;

----- 完整解决方案 -------------------------------------------------------- --------------

在类的OnCreate方法中

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.signin_page);
    setProgressBarIndeterminateVisibility(false); //Sets Default value
    //LOOK at Picture #1 for my screen in this state

}

OnClick方法中(我为此活动实现了 setOnClickLister)

@Override
public void onClick(View v) {

    setProgressBarIndeterminateVisibility(true);

    switch(v.getId()){


    case R.id.verify_button:


        String gmailString = gmail.getText().toString();
        String passString = password.getText().toString();

        new AsyncTask<String,Void,Boolean>() {
            protected void onPreExecute() {
                setProgressBarIndeterminateVisibility(true);
                        //LOOK at Picture #2 for my screen in this state
            }

            protected Boolean doInBackground(String... args) {
                String gmailString = args[0];
                String[] recip = new String[] { gmailString };
                String passString = args[1];
                String body = args[2];

                MailAccount a  = new MailAccount(gmailString, passString);
                try {
                    return a.sendEmailTo(recip, "test", body);
                } catch (MessagingException e) {
                    Log.e("failed to connect", "mess: "+e.getMessage());
                    return false;
                } 
            }

            protected void onPostExecute(Boolean isGoogleAuthenticated) {
                setProgressBarIndeterminateVisibility(false);
                if(isGoogleAuthenticated){
                    email_success.setVisibility(View.VISIBLE);
                    password_success.setVisibility(View.VISIBLE);
                    signin_button.setEnabled(true);
                    password.setEnabled(false);
                    gmail.setEnabled(false);

                    gmail.setBackgroundResource(R.layout.bordersuccess);
                    password.setBackgroundResource(R.layout.bordersuccess);
                }else{
                    gmail.setText("");
                    password.setText("");
                }
            }
        }.execute(gmailString, passString, "test complete");
        //LOOK at Picture #3 for my screen in this state

        break;

图片#1 onCreate

图片#1 onCreate


图片#2 onClick - 正在加载

图片#2 onClick - 正在加载


图#3完成加载

图#3 完成加载

4

1 回答 1

3

在您的 onClick 方法中,您将需要使用AsyncTask。使用 AsyncTask 意味着您无需担心编写自己的线程代码。如评论中所述,您正在执行的连接将阻塞主线程,可能导致您的应用停止响应。也许你会想要一些类似的东西;

@Override
public void onClick(View v) {
    String gmailString = gmail.getText().toString();
    String passString = password.getText().toString();

    new AsyncTask<String,Void,Boolean>() {
        protected void onPreExecute() {
            setProgressBarIndeterminateVisibility(true);
        }

        protected Boolean doInBackground(String... args) {
            String gmailString = args[0];
            String[] recip = new String[] { gmailString };
            String passString = args[1];
            String body = args[2];

            MailAccount a  = new MailAccount(gmailString, passString);
            try {
                return a.sendEmailTo(recip, "test", body);
            } catch (MessagingException e) {
                Log.e("failed to connect", "mess: "+e.getMessage());
                return false;
            } 
        }

        protected void onPostExecute(Boolean isGoogleAuthenticated) {
            setProgressBarIndeterminateVisibility(false);
            // Do the rest of your UI updates here
        }
    }.execute(gmailString, passString, body);
}
于 2013-08-10T18:50:07.830 回答