gpt4 book ai didi

android - Android-将不确定的ProgressBar与另一个线程一起使用

转载 作者:行者123 更新时间:2023-12-03 13:20:27 25 4
gpt4 key购买 nike

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

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;

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

in 中的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 方法中(我为此 Activity 实现了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 完成加载

最佳答案

在您的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);
}

关于android - Android-将不确定的ProgressBar与另一个线程一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com