Android AsyncTask 和登录
Android AsyncTask and Login
我正在编写一个 Android 登录页面,我使用了一个扩展 AsyncTask 的 Class。
问题是我似乎无法 "control" 异步任务的流程。
如果我输入正确的用户名和密码,如果我在连接到数据库时单击一次,它会说它们不正确,但如果我再按一次,它就可以了。
幸运的是,当我输入错误的用户或密码时不会发生这种情况。
我使用了一个布尔变量来显示密码和用户名是否正确。
public class LoginPage extends Activity implements OnClickListener {
public boolean loginCorrect=false;
//other code
}
然后我实现了 OnClick(View v) 函数:
public void onClick(View v)
{//switch related code
case(R.id.btn_submit):{
connectToDB(v);
if(loginCorrect)
{loginCorrect=false // If i have to login another time, it will make all the needed checks
Intent intent = new Intent(this, MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
else
{Toast.makeText(getApplicationContext(),"Email or password is incorrect",
Toast.LENGTH_SHORT).show();
}
break;
//other code
在 connectToDb(v) 中:
public void connectToDB(View view)
{
ConnectAsync task = new ConnectAsync();
task.execute();
}
并在 doInBackground
@Override
protected String doInBackground(String... urls) {
String url= //jdbc string
String user=insUser.getText().toString();
String password=insPw.getText().toString();
Connection connessione=null;
Statement statement=null;
ResultSet resultSet=null;
String sql=//sql query
try{ Class.forName //driver loading
connessione=DriverManager.getConnection(url,user,password);
statement=connessione.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next())
if( //i found a tuple with that username and that password
{
loginCorrect=true;
}
connessione.close();
}
catch (SQLException se){
//exception related code
}
catch (ClassNotFoundException cnfe) {
//exception related code
}
return null;
}
问题是根据定义 AsyncTask
不是 UI 线程上的 运行。所以通过这样做:
connectToDB(v);
if(loginCorrect)
您不会等到 AsyncTask
结束才检查值。我建议您实现 AsyncTask
的 onPostExecute
方法,该方法将在 AsyncTask
结束后在 UI 线程上调用。
像这样:
@Override
protected void onPostExecute(YOUR_TYPE result) {
if(loginCorrect)
{loginCorrect=false // If i have to login another time, it will make all the needed checks
Intent intent = new Intent(this, MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
else
{Toast.makeText(getApplicationContext(),"Email or password is incorrect",
Toast.LENGTH_SHORT).show();
}
}
在异步任务完成之前,您永远不会检查用户是否再次按下按钮。您可以设置 btn_submit.setClickable(false)
,然后在您的任务完成后将其设置回 true。只需覆盖 onPostExecute(...)
并在其中插入 btn_submit.setCLickable(true)
即可。
有什么问题吗?
首先你需要覆盖 AsyncTask 的 onPostExecute
函数
处理您的查询结果。您可以从 Activity 调用一个函数或在 onPostExecute.
中添加代码
class ConnectToDBTask extends AsyncTask<Void,Void,Boolean>
{
@Override
protected Boolean doInBackground(Void... params)
{
Boolean bResult = false;
try
{
bResult = true;
}
catch (Exception se)
{
}
return bResult;
}
@Override
protected void onPostExecute(Boolean aBoolean)
{
if ( aBoolean == true)
{
Main.this.HandleLoginResult(aBoolean);
}
super.onPostExecute(aBoolean);
}
}
然后您可以在 activity 中添加一个函数,如果登录成功,该函数将调用意图。
public void HandleLoginResult(Boolean bResult)
{
if ( bResult == true)
{
Intent intent = new Intent(this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
}
我正在编写一个 Android 登录页面,我使用了一个扩展 AsyncTask 的 Class。
问题是我似乎无法 "control" 异步任务的流程。
如果我输入正确的用户名和密码,如果我在连接到数据库时单击一次,它会说它们不正确,但如果我再按一次,它就可以了。
幸运的是,当我输入错误的用户或密码时不会发生这种情况。
我使用了一个布尔变量来显示密码和用户名是否正确。
public class LoginPage extends Activity implements OnClickListener {
public boolean loginCorrect=false;
//other code
}
然后我实现了 OnClick(View v) 函数:
public void onClick(View v)
{//switch related code
case(R.id.btn_submit):{
connectToDB(v);
if(loginCorrect)
{loginCorrect=false // If i have to login another time, it will make all the needed checks
Intent intent = new Intent(this, MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
else
{Toast.makeText(getApplicationContext(),"Email or password is incorrect",
Toast.LENGTH_SHORT).show();
}
break;
//other code
在 connectToDb(v) 中:
public void connectToDB(View view)
{
ConnectAsync task = new ConnectAsync();
task.execute();
}
并在 doInBackground
@Override
protected String doInBackground(String... urls) {
String url= //jdbc string
String user=insUser.getText().toString();
String password=insPw.getText().toString();
Connection connessione=null;
Statement statement=null;
ResultSet resultSet=null;
String sql=//sql query
try{ Class.forName //driver loading
connessione=DriverManager.getConnection(url,user,password);
statement=connessione.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next())
if( //i found a tuple with that username and that password
{
loginCorrect=true;
}
connessione.close();
}
catch (SQLException se){
//exception related code
}
catch (ClassNotFoundException cnfe) {
//exception related code
}
return null;
}
问题是根据定义 AsyncTask
不是 UI 线程上的 运行。所以通过这样做:
connectToDB(v);
if(loginCorrect)
您不会等到 AsyncTask
结束才检查值。我建议您实现 AsyncTask
的 onPostExecute
方法,该方法将在 AsyncTask
结束后在 UI 线程上调用。
像这样:
@Override
protected void onPostExecute(YOUR_TYPE result) {
if(loginCorrect)
{loginCorrect=false // If i have to login another time, it will make all the needed checks
Intent intent = new Intent(this, MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
else
{Toast.makeText(getApplicationContext(),"Email or password is incorrect",
Toast.LENGTH_SHORT).show();
}
}
在异步任务完成之前,您永远不会检查用户是否再次按下按钮。您可以设置 btn_submit.setClickable(false)
,然后在您的任务完成后将其设置回 true。只需覆盖 onPostExecute(...)
并在其中插入 btn_submit.setCLickable(true)
即可。
有什么问题吗?
首先你需要覆盖 AsyncTask 的 onPostExecute
函数
处理您的查询结果。您可以从 Activity 调用一个函数或在 onPostExecute.
class ConnectToDBTask extends AsyncTask<Void,Void,Boolean>
{
@Override
protected Boolean doInBackground(Void... params)
{
Boolean bResult = false;
try
{
bResult = true;
}
catch (Exception se)
{
}
return bResult;
}
@Override
protected void onPostExecute(Boolean aBoolean)
{
if ( aBoolean == true)
{
Main.this.HandleLoginResult(aBoolean);
}
super.onPostExecute(aBoolean);
}
}
然后您可以在 activity 中添加一个函数,如果登录成功,该函数将调用意图。
public void HandleLoginResult(Boolean bResult)
{
if ( bResult == true)
{
Intent intent = new Intent(this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
finish();
startActivity(intent);
}
}