我试试这个教程
我仍然很困惑如何使用 asynctask 在按钮注销中添加进度对话框
这是我的活动。
package hariff.ltis.mainmenu;
import hariff.ltis.inputhama.CInputHamaApp;
import hariff.ltis.library.DatabaseHandler;
import hariff.ltis.library.UserFunctions;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import hariff.ltis.mainmenu.R;
public class MainMenu extends Activity{
UserFunctions userFunctions;
Button btnLogout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
setContentView(R.layout.main);
btnLogout = (Button) findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
loadUser();
View button3Click = findViewById(R.id.button3);
button3Click.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// Perform action on click
DataHama();
}
});
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
/*private void InputData(){
Intent i = new Intent(this, SaveData.class);
startActivity(i);
}
private void CheckData(){
Intent i1 = new Intent(this, CheckData.class);
startActivity(i1);
}*/
private void loadUser() {
TextView txt = (TextView) findViewById(R.id.textUser);
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
SQLiteDatabase dbs = db.getReadableDatabase();
Cursor cursor = dbs.rawQuery("SELECT * FROM login", null);
if (cursor.moveToFirst()) {
do {
String emailid=cursor.getString(2); // Here you can get data from table and stored in string if it has only one string.
txt.setText(emailid);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
if(db!=null)
{
db.close();
}
// Creating adapter for spinner
}
private void DataHama(){
Intent i2 = new Intent(this, CInputHamaApp.class);
startActivity(i2);
}
}
所以当我单击按钮注销时,进度对话框显示并有时间,例如 5 秒。怎么做?
按钮注销编辑:
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
logoutUserOperation loguttask = new logoutUserOperation(MainMenu.this);
loguttask.execute("");
}
});
这是 logoutUser 函数
/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}
编辑:这是异步任务
private class logoutUserOperation extends AsyncTask<String, Void, Boolean> {
ProgressDialog progressDialog;
Context context;
public logoutUserOperation(Context context){
this.context=context;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "", "Logout...");
}
@Override
protected Boolean doInBackground(String... params) {
boolean boollogout=userFunctions.logoutUser(context);
return boollogout;
}
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
if(result){
Intent login = new Intent(context,LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(login);
// Closing dashboard screen
context.finish();
}else{
// your code here...
}
}
}
当我尝试但显示错误时
The method finish() is undefined for the type Context
BR
亚历克斯
编辑:这是睡眠 5 秒的代码
protected Boolean doInBackground(String... params) {
boolean boollogout=userFunctions.logoutUser(context);
try{
Thread.sleep( 5000 );
} catch( Exception e ){
Log.i("Logout", e.getMessage() );
}
return boollogout;
}