gpt4 book ai didi

android - onPostExecute() 等待时间过长?

转载 作者:行者123 更新时间:2023-11-29 18:04:59 29 4
gpt4 key购买 nike

我有一些控制权,因此我做了一些线程。我想用 asycntask 将一个屏幕的加载栏显示到另一个屏幕。我的问题是加载栏正在快速显示和消失,然后在没有加载的情况下等待同一个屏幕很多bar.And 一段时间后它会进入第二个屏幕。我认为后执行不适合我。有人可以帮助我吗?

这是我的代码:

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void>{

/** This callback method is invoked, before starting the background process */
@Override
protected void onPreExecute() {
super.onPreExecute();
/** Creating a progress dialog window */
mProgressDialog = new ProgressDialog(Login.this);

/** Close the dialog window on pressing back button */
mProgressDialog.setCancelable(true);

/** Setting a horizontal style progress bar */
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

/** Setting a message for this progress dialog
* Use the method setTitle(), for setting a title
* for the dialog window
* */
mProgressDialog = ProgressDialog.show(Login.this,
"Giriş yapıyorsunuz", "Lütfen bekleyin...");



}

/** This callback method is invoked on calling execute() method
* on an instance of this class */
@Override
protected Void doInBackground(Void...params) {

try {
startMyApplication() ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/** This callback method is invoked when publishProgress()
* method is called */
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
mProgressDialog.setProgress(mProgressStatus);
}

/** This callback method is invoked when the background function
* doInBackground() is executed completely */
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (mid > 0) {
Intent btn_login = new Intent(
getApplicationContext(), MainScreen.class);
startActivity(btn_login);
}
else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this);

// Setting Dialog Title
alertDialog.setTitle("GİRİŞ");

// Setting Dialog Message
alertDialog.setMessage("Kullanıcı adı veya Parola Hatalı Lütfen tekrar deneyin.");

// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);

// Setting Negative "NO" Button
alertDialog.setNegativeButton("TAMAM", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
});

// Showing Alert Message
alertDialog.show();
}

//run();
mProgressDialog.dismiss();

这是 startmyapp theads:

public void startMyApplication() throws InterruptedException {

ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<String> futureOne = new FutureTask<String>(
new Callable<String>() {
public String call() throws Exception {
if (isOnline()) {
// Call Login Web Service with username and password and get mid
mid = callLoginWS(username.getText().toString(), password.getText().toString());
if (mid > 0) {
//callPasswordGeneratorWS(mid);
// Insert mid and username into sqllite
dbInstance.insertMerch(mid,username.getText().toString());
}
Log.i("futureone", "futureone");

}
return "TEST";
}
});

FutureTask<String> futureTwo = new FutureTask<String>(
new Callable<String>() {
public String call() throws Exception {
// Get mid from database
mid = dbInstance.selectMerch(username.getText().toString());
return "TEST";
}

});



// ... Dispatch
// Check if user exists previously
// ... Dispatch
mid = dbInstance.selectMerch(username.getText().toString());
dbInstance.close();


executor.execute(futureOne);
while (!(futureOne.isDone())) {
executor.execute(futureTwo);
}
Log.i("login","new login");
//}
executor.shutdown();
}

最佳答案

此部分导致延迟。

while (!(futureOne.isDone())) {
executor.execute(futureTwo);
}

在这里,您正在阻止当前线程完成。并等待 futureOne 完成。

关于android - onPostExecute() 等待时间过长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837208/

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