我正在尝试使用从用户单击按钮到发送电子邮件时的不确定进度条。我遇到的问题是我打开以发送电子邮件和我认为的进度条。现在它只是崩溃,当两个人在一起时,我不确定是否有一个聪明的方法来启用进度条(基本上我只是想要发送电子邮件的旋转圈的动画,以便用户有一些反馈背景中发生的事情。)
的OnCreate
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);
}
的OnClick
@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
图片#2 onClick - 正在加载
图片#3 已完成加载
答案 0 :(得分:2)
在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);
}