i have created one simple login application which takes user name and password from sqlserver..it works fine...
i want during login process one progeress bar should be displayed using asyntask...
but i am unaware to use parameters in asyntask...if some one plzz tell me how to put my method in doInbackground of asyntask and what param should i use....
my code is;.....
public void save(){
initilize();
ResultSet rs = null;
String mylog=id.getText().toString();
String mypass=pass.getText().toString();
try{
Statement statement=connect.createStatement();
rs=statement.executeQuery("LOGIN '"+mylog+"', '"+mypass+"'");
}catch(Exception e){
e.printStackTrace();
}
if(mylog.equals("")||mypass.equals("")){
Toast.makeText(getApplicationContext(), "empty fields", Toast.LENGTH_SHORT).show();
} else
try {
if(rs.next()){
Intent i=new Intent(getApplicationContext(),Act2.class);
startActivity(i);
}
else if(rs.next()==false){
Toast.makeText(getApplicationContext(), "incorrect login", Toast.LENGTH_SHORT).show();
id.setText("");
pass.setText("");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if it is possible that same method save() be kept inside doInbackground() of asyntask...
making a fast refactorization (note that this as it stand it's really bad practice and coding, you MUST refactor this code to be more maintanable and to avoid duplication):
public class MyAsyncTask extends AsyncTask<> {
private Activity activity;
boolean result;
private String myLog;
private String myPass;
private Connection connect;
public MyAsyncTask(Activity activity, Connection connect) {
this.activity = activity;
this.connect = connect;
}
#Override
protected void onPreExecute() {
//show your progress dialog
}
#Override
protected Object doInBackground(Object[] objects) {
ResultSet rs = null;
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
initilize();
mylog=id.getText().toString();
mypass=pass.getText().toString();
}
});
try{
Statement statement=connect.createStatement();
rs=statement.executeQuery("LOGIN '"+mylog+"', '"+mypass+"'");
}catch(Exception e){
e.printStackTrace();
}
if(mylog.equals("")||mypass.equals("")){
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(activity.getApplicationContext(), "empty fields", Toast.LENGTH_SHORT).show();
}
});
} else
try {
if(rs.next()){
result = true;
}
else if(rs.next()==false){
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(activity.getApplicationContext(), "incorrect login", Toast.LENGTH_SHORT).show();
id.setText("");
pass.setText("");
}
});
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Object o) {
//hide your progress dialog
if(result == Boolean.TRUE){
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Intent i=new Intent(activity.getApplicationContext(),Act2.class);
activity.startActivity(i);
}
});
}
}
}
then in your Activity you do this:
MyAsyncTask a = new MyAsyncTask(this, connect); //im guessing "connect" is your Connection object
a.execute();
As i said i made this fast refactoring for the code to work but best practice and good implementation is not in consideration here.
Maybe, you could use a timer, to check if your
login is ready. As long as it is not, you Show your progress Bar. If its ready, you can close the Bar and start a new activity or anything. timers run can run on u UI thread.
regards :)
Related
i am trying to display a Toast on the screen and when Toast fades off then move to the next question. I have tried with Thread but cannot seem to manage.
My code:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getUserSelection()){
position = position + 3;
if (position < questionsArray.size()) {
curName = questionsArray.get(position).getName();
curArray = questionsArray.get(position).getAnswers();
curIscorrect = questionsArray.get(position).getIscorrect();
setupQuestionView(curName, curArray, curIscorrect);
} else {
StringGenerator.showToast(QuestionsActivity.this, "Your score : " + score + "/" + (questionsArray.size() / 3));
}
}else {
StringGenerator.showToast(QuestionsActivity.this, getString(R.string.noanswerselected));
}
}
});
and the getUserSelectionMethod:
private boolean getUserSelection() {
correct = (RadioButton)findViewById(group.getCheckedRadioButtonId());
if (correct == null){
return false;
}else {
correctAnswerText = correct.getText().toString();
if (map.get(correctAnswerText).equals(Constants.CORRECTANSWER)) {
score++;
setCorrectMessage();
return true;
} else {
setWrongMessage();
return true;
}
}
}
private void setCorrectMessage() {
correctToast = new Toast(QuestionsActivity.this);
correctToastView = getLayoutInflater().inflate(R.layout.correct, (ViewGroup) findViewById(R.id.correctRootLayout));
correctText = (TextView)correctToastView.findViewById(R.id.correctTextView);
correctText.setText(getString(R.string.correctAnswer));
correctToast.setDuration(Toast.LENGTH_SHORT);
correctToast.setView(correctToastView);
correctToast.show();
correctThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
correctToast.cancel();
}
});
correctThread.start();
}
private void setWrongMessage() {
wrongToast = new Toast(QuestionsActivity.this);
wrongToastView = getLayoutInflater().inflate(R.layout.wrong, (ViewGroup) findViewById(R.id.wrongRootLayout));
wrongText = (TextView)wrongToastView.findViewById(R.id.wrongTextView);
wrongText.setText(getString(R.string.wrongAnswer));
wrongToast.setDuration(Toast.LENGTH_SHORT);
wrongToast.setView(wrongToastView);
wrongToast.show();
wrongThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
wrongToast.cancel();
}
});
wrongThread.start();
}
Any suggestion on how to do this?
You can determine the toast visibility:
toast.getView().getWindowToken()
If the result is null, than your toast isn't visible anymore, and than you can run any code you want.
as stated in this answer you can start a thread that waits the duration of the Toast:
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(3500); // 3.5seconds!
// Do the stuff you want to be done after the Toast disappeared
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Toast.LENGTH_SHORT and Toast.LENGTH_LONG are only flags so you have to either hard code the duration or keep them in a constant. The durations are 3.5s (long) and 2s (short).
If you want to manipulate some of your views, you cannot do this in another thread than the "main" UI thread. So you have to implement a kind of callback/polling mechanism to get notified when the SleepThread has finished.
Check this answer to read about a couple of ways to do this. Probably the easiest of them to understand and implement is this:
After you started your Thread you can check if it is still alive and running by calling thread.isAlive(). In this way you can do a while loop that runs while the thread is running:
// start your thread
while(thread.isAlive()){}
// continue the work. The other thread has finished.
Please note that this is NOT the most elegant way to do this! Check the other possibilities in the answer I've mentioned above for more elegant solutions (especially the last one with the listeners is very interesting and worth reading!)
That's because the Thread class is purely executed in the background and you need to manipulate the view in the Main thread. To solve your issue just replace the Thread with AsynTask.
AsyncTask<Void,Void,Void> a = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
correctToast.cancel();
}
};
a.execute();
If you look at my code you can see my onPostExecute, this method is called in the Main Thread.
My Error was because i was trying to acess UI Elements through another Thread so modifying the code like this:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
QuestionsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
moveToNextQuestion();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
did the trick. I hope my answer helps someone!!!
I would like to show ProgressDialog during another asynctask.
I write ProgressDialog . one AsyncTask
and I write another AsyncTask in PostExecute method.
but ProgressDialog doesn't run... I'd appreciate your kind help.
AsyncTask code:
public class SavingProgressTask extends AsyncTask<Void, Void, Void> {
private Context mContext;
private ProgressDialog progDialog = null;
public SavingProgressTask(Context context) {
mContext = context;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progDialog = new ProgressDialog(mContext);
progDialog.setMessage("saving...");
progDialog.setIndeterminate(false);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setCancelable(false);
progDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(isUpdateStore) {
try {
int storeResponseMessage = new StoreUpdateTask(SettingActivity.this).execute(fc_code).get();
if(storeResponseMessage == 1) {
Store updateStore = new StoreDetailTask(SettingActivity.this, false).execute(fc_code).get();
mAuthUser.setStore(updateStore);
UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
int userinfoResponseMessage;
int userinfosex;
if(mSexTextView.getText().toString().equals("남자")) {
userinfosex = 1;
} else if (mSexTextView.getText().toString().equals("여자")){
userinfosex = 2;
} else {
userinfosex = 2;
}
if(isUpdateBirthday) {
if(isUpdateSex) {
try {
userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "TWICE").execute(tempBirthDay, String.valueOf(userinfosex)).get();
if(userinfoResponseMessage == 1) {
mAuthUser.getUser().setBirthday(tempBirthDay);
mAuthUser.getUser().setSex(userinfosex);
UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
try {
userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "BIRTHDAY").execute(tempBirthDay).get();
if(userinfoResponseMessage == 1) {
mAuthUser.getUser().setBirthday(tempBirthDay);
UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} else {
if(isUpdateSex) {
try {
userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "SEX").execute(String.valueOf(userinfosex)).get();
if(userinfoResponseMessage == 1) {
mAuthUser.getUser().setSex(userinfosex);
UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
}
}
if(progDialog != null) {
progDialog.dismiss();
Intent intent = new Intent(mContext, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
}
in click listener:
SavingProgressTask saveTask = new SavingProgressTask(SettingActivity.this);
saveTask.execute();
As far as I know starting another async task from the post execute of previous async task is not that good idea.
When I had a similar problem in one of my projects I had followed the following process.
1) Create an interface with a method like notifyUIThread().
2) Implement this interface from the activity, which calls the async task.
3) While creating the async task, pass 'this' as a parameter to the constructor of the async task.
4) In the post execute of async task call the this.notifyUIThread() method.
5) In the overridden method notifyUIThread(), make a call to the next async task.
Coming to the progress dialog, make it global variable, show it in the pre execute method of the first async task, dismiss it in the post execute of the second async task.
You can also do make the progress dialog local to the async task, show in pre execute and dismiss in post execute of both the async tasks.
But a small advice here is, keep the app responsive. Showing the progress dialog for a long time is not advisable.
I have 2 Asynctask, 1 for get data (location) from server then set a marker on map with this location and another call 1st Asyntask in a loop for updating location.
Here my code:
public class AsynComp extends AsyncTask<Void, Void, Void> {
ProgressDialog taxiDialog;
#Override
protected Void doInBackground(Void... params) {
jsonComp = new JSONComp(find_url);
find_status = jsonComp.getJsonStatus(txt_search);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (find_status.equals("2013")) {
Toast.makeText(getBaseContext(), "no result",
Toast.LENGTH_SHORT).show();
} else if (find_status.equals("2012")) {
for (Marker marker:markers){
if(marker.getTitle().equals(compFollow)){
marker.remove();
}
}
for (int i=0; i<number;i++){
comp = new Comp(jsonComp.getJsondata(i));
SetMarkerComp(comp);
try {
Thread.sleep(1400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class AsynFollow extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
if (!taxiFollow.equals("")) {
number = 1;
txt_search = compFollow;
find_url = "http://192.111.125.80:8001/Default.aspx?username="
+ Id + "&password=" + Pass + "&sohieuxe="+txt_search;
while (!stop){
new AsynComp().execute();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
taxiFollow = "";
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (!compFollow.equals("")) {
Toast.makeText(getBaseContext(), "Follow "+compFollow, Toast.LENGTH_SHORT).show();
} else {
iv_theodoi.setVisibility(View.VISIBLE);
iv_theodoif.setVisibility(View.GONE);
Toast.makeText(getBaseContext(), "Plz choose a marker", Toast.LENGTH_SHORT).show();
}
}
}
And i have 2 buuton, 1 to call AsynFollow.execute(), another to stop it.
This code can run but app will force close after awhile.
Any solution? thanks.
P/s: i'm a newbie in android.
You shoulnd you asyncTask for this. For repetitive action, like changing status in some interval, use Timer class. In this way you can implement repetitive action which can be repeated in intervals.
In this way you can stop this time by on click listener. You can run two times and specify it's realtions using other variables.
If you're newbe, you should read about multitasking in Android: Timer, AsyncTask, Handler.
In my opinion this docs will tell you much more than thousands of comments in stackoverflow.
Here is my code segment I am trying to dismiss dialog but it is not getting dismissed and also i don't get any error on logcat.Please correct me where i am wrong ?
All the Log.v statement get executed.Even log.v statement after pd.dismiss() (Log.v("TAG","progress dismiss");) gets printed.
Please point my mistake or suggest some alternative way to dismiss the progressdialog.
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//do something
} else {
Log.v("TAG","above progressDialog");
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
new Thread() {
public void run() {
try {
sleep(2000);
Log.v("TAG","in try block");
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
pd.dismiss();
// Log.v("TAG","progress dismiss");
}
}.start();
Log.v("TAG","after start");
public void run()
{
try{
if (!(txtOldPass.getText().toString())
.equals(SetGetValues.getPassword())) {
Toast.makeText(ChangePassword.this,
"Invalid Old Password", Toast.LENGTH_SHORT)
.show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
if (!(txtNewPass.getText().toString())
.equals(txtCnfPass.getText().toString())) {
Toast.makeText(ChangePassword.this,
"Re-Enter New Password",
Toast.LENGTH_SHORT).show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
try {
handler = new Handler();
handler.postDelayed(new Thread (new Runnable(){
JSONStringer loginuser = new JSONStringer()
.object()
.key("userid")
.value(SetGetValues.getUserid()
.trim())
.key("password")
.value(txtCnfPass.getText()
.toString().trim())
.key("oldpassword")
.value(txtOldPass.getText()
.toString().trim())
.endObject();
StringEntity entity = new StringEntity(
loginuser.toString());
JSONObject results = bc
.returnJSONObject(loginuser,
"url");
String message = results
.getString("message");
String isvalid = results
.getString("isvalid");
if (isvalid.contains("FALSE")) {
} else {
//pd.dismiss();
Toast.makeText(
ChangePassword.this,
"Password changed successfully",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}), 1000);
}
}
});
You are not dismissing the same ProgressDialog that you are showing. Replace this:
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
with this:
final ProgressDialog pd = ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
if (pd.isShowing()) {
pd.dismiss();
}
Instead of using thread Use Async Task
usage
You problem probably is, that you're trying to modify the UI (dismiss the dialog) from another thread. I'm surprised that you don't get an error message. Try dismissing the dialog from the UI thread, for example by using .runOnUIThread().
Because you are trying to use element of UI Thread progressDialog in a non UI thread .
This will be allowed through AsyncTask , handler or RunOnUIThread(runnable) only .
read more about them and use any one .
progressDialog = ProgressDialog.show(GetResponse.this, "", "Loading...");
new Thread()
{
public void run()
{
try
{
// inside i have written code for making connection to the server using SSL connection.
}catch (Exception e)
{
progressDialog.dismiss();
exception(e.getMessage())
}.start();
}
private void exception(String msg)
{
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
this.finish();
Intent i = new Intent(getBaseContext(), LoginPage.class);
startActivity(i);
}
my LoginPage.java is previous activity.
If the connection is successfull it goes to the next activity ot doesnt give any error,
But if der is any prob with connection then i want progress bar should be stopped and go back to the LoginPage activity and also i want the error msg to be displayed.
From the above im getting some error.. Please help me out on this
Pass in and use the context from LoginPage. Also, use the 101010 button to format your code as code in your posts.
you can go up by using try catch mechanism where in your catch place your toast message and u can do it also by asynchronous task,
here simple code
private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
Bru_Sports_View.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
//here the condition to check login details
}
} catch (Exception e) {
}
return null;
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
and u can also use try,catch in catch block you can place your toast message
with finsih() method