このチュートリアルを試してみます
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;
}