i have an asynctask but if i implement a Thread.Sleep , then my app crashes , i dont know why, in onPreExecute i can see my first message and then after two secs it appears the other one i put in doInBackground , but its not working
private class sendMail extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
dialog.setMessage("Please wait...");
dialog.show();
}
// automatically done on worker thread (separate from UI thread)
#Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.setMessage("Downloading files...");
new BackgroundTask().execute();
//MY DOWNLOADING METHODS STUFF
And then i dismiss this dialog somewhere else
Log
An error occurred while executing doInBackground()
You cannot access the UI elements from a background thread you can update the progressbar in onProgressUpdate, most importantly you need to publishProgress(value) in doInBackground and update using onProgressUpdate. Read more about the AsyncTask here.
Example code:
class MyTask extends AsyncTask<Integer, Integer, String> {
#Override
protected String doInBackground(Integer... params) {
for (; count <= params[0]; count++) {
try {
Thread.sleep(1000);
publishProgress(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "Task Completed.";
}
#Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
txt.setText(result);
btn.setText("Restart");
}
#Override
protected void onPreExecute() {
txt.setText("Task Starting...");
}
#Override
protected void onProgressUpdate(Integer... values) {
txt.setText("Running..."+ values[0]);
progressBar.setMessage("Downloading files...");
progressBar.setProgress(values[0]);
}
}
Use UI thread to show dialog message in doInBackground refer this https://stackoverflow.com/a/15757788/6626966
Related
I have the code that sends requests to REST API in AsyncTask.
Also, I have a ProgressDialog initialization in preExecute() and its dismission in postExecute().
I want ProgressDialog to show an indeterminate spinner (you know, that loading animation), but I need to get a result too. get() blocks the main thread where I'm invoking it in - what's the workaround for that case?
Main thread (main activity)
LoginTask task_login = new LoginTask();
AsyncTask<String, Void, JSONObject> response = task_login.execute(et_username.getText().toString(), et_password.getText().toString());
try {
JSONObject json = response.get();
Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
Toast.makeText(MainActivity.this, "Interrupted.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
AsyncTask (dummy doInBackground):
public class LoginTask extends AsyncTask<String, Void, JSONObject> {
private LoginTask self = this;
private ProgressDialog progressDialog;
#Override
protected JSONObject doInBackground(String... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.context,
"Logging in...", "");
progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
MainActivity.context.getResources().getResourceEntryName(R.string.dialog_button_cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
self.cancel(true);
}
});
}
#Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
progressDialog.dismiss();
}
}
You can use AsyncTask's onProgressUpdate() method to perform the actions on the UI thread (such as showing or updating a loading animation) while doInBackGround() does the background work on another thread.
Basically you invoke the publishProgress() method from within doInBackGround(), which in turn calls onProgressUpdate().
Check out the Android reference page on AsyncTask for an example.
Please look at the usage of AsyncTask https://developer.android.com/reference/android/os/AsyncTask#usage
There is a callback function onPostExecute which returns (as parameter) the value you requested:
private class RestTask extends AsyncTask<Object, Object, String> {
protected String doInBackground(Object... args) {
// this happend on background thread
return downloadData();
}
protected void onPreExecute() {
// this happend on UI thread
showSpinner();
}
protected void onPostExecute(String result) {
// this happend on UI thread
hideSpinner();
doSomethingWithDownloadResult(result);
}
}
Usage:
new RestTask().execute()
As you edited the question, this:
try {
JSONObject json = response.get();
Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
Toast.makeText(MainActivity.this, "Interrupted.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
should be called in previous Tasks onPostExecute method, this way you will not block you UI with get method waiting on login result.
1 - You can use a callback method.
but keep in mind you should call it in your main thread.
2 - you can use LocalBroadcastManager in order to send your result through Intent.
3 - you might want to use in application messaging libraries which are more reliable in my opinion. one example which I use very often is EventBus.
In my android app, i am doing time consuming task extending AsyncTask, and want to display the progress in Toast messages. Toast messages are also displayed onPre() and onPost().
I am able to display Toast messages onPre() & onPost() but not able to show onProgressUpdate(Integer... progress).
Following is my code...
public class MainClass extends Activity {
public void Start(View view) {
DemoTasks runner = new DemoTasks(this);
runner.execute("Start");
}
private class DemoTasks extends AsyncTask<String, Integer, Integer> {
private Context context;
public DemoTasks(Context context){
this.context = context;
}
#Override
protected Integer doInBackground(String... params) {
try {
publishProgress(0);
doWork();
Thread.sleep(5000L);
publishProgress(100);
} catch (Exception localException) {
Log.d("POST", localException.getMessage());
}
return 100;
}
#Override
protected void onPostExecute(Integer result) {
Toast.makeText(context, "post", Toast.LENGTH_SHORT).show();
}
#Override
protected void onPreExecute() {
Toast.makeText(context, "pre", Toast.LENGTH_SHORT).show();
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
Toast.makeText(context, "progress-" + progress, Toast.LENGTH_SHORT).show();
}
}
}
Also in my doInBackgroud(String...params) ...Thread.sleep is also not working.
As soon as onPre() gets executed, onPost() also executes after that!!!!
You can try this,
showProgress ();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dialog.cancel();
Intent i=new Intent(getApplicationContext(),Main.class);
startActivity(i);
finish();
}
}, 3000); //number of seconds
private ProgressDialog dialog;
public void showProgress () {
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Please wait");
dialog.show();}
Bascially,you can access the UI on any method, even In doinBackground you can access the UI using runOnUIthread.
here is one AsyncTask Example. This will show a peogress dialog while executing the task.
private class LoginProcessing extends AsyncTask<Object, Void, Void> {
private LoginCredentials myLoginCredentials;
private ProgressDialog progressDialog;
public LoginProcessing(LoginCredentials Credentials) {
super();
myLoginCredentials=Credentials;
progressDialog.setMax(100);
progressDialog.setMessage("Please Wait..");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.show();
}
protected void onPreExecute (){
}
#Override
protected Void doInBackground(Object... arg0) {
// TODO Auto-generated method stub
//Code to do the process in background
return null;
}
#Override
protected void onProgressUpdate(Long... progress) {
// int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
progressDialog.setProgress(progress);
}
protected void onPostExecute(Void result){
progressDialog.dismiss();
//Your code after the process
}
}
You can call this Task as,
new LoginProcessing(loginCredentials).execute();
In this Example loginCredentials is the parameter I am passing to the AsyncTask. You can change it to your own parameter.
I have a AsyncTask<Client, Void, Boolean> called LoginTask with a ProgressDialog, which you can see here.
Now my problem is, if I launch this task with LoginTask(...).execute().get() the ProgressDialog is only shown very short at the end of the AsyncTask. Also I've put in some Thread.sleep(), but if I do that, I also get a ProgressDialog at the end of the Thread.
How can the ProgressDialog be displayed the whole async task?
public class LoginTask extends AsyncTask<Client, Void, Boolean>
{
private ProgressDialog progressDialog;
private MainActivity activity;
public LoginTask(MainActivity activity)
{
this.activity = activity;
}
#Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(activity);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(activity.getText(R.string.asynctask_login_connect));
progressDialog.setCancelable(false);
progressDialog.show();
}
#Override
protected Boolean doInBackground(Client... params)
{
try
{
Client client = params[0];
if (client.connect())
{
progressDialog.setMessage(activity.getText(R.string.asynctask_login));
if (client.manageHandshake())
{
return true;
}
else
return false;
}
else
return false;
}
catch (Exception e)
{
Log.e(LoginTask.class.getSimpleName(), "Can not handshake and connect to/with server.", e);
return false;
}
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
}
}
From the docs of the get(),
Waits if necessary for the computation to complete, and then retrieves
its result.
This means that it will block the calling Thread until the execution is done; this defeats the whole purpose of using AsyncTask. Just call execute() and update the UI Thread from onPostExecute()
I have an AsyncTask in a class. I want it to display the progress while it is getting executed. But it is not printing the Logs.
private void registerBackground() {
new AsyncTask<Void, Integer, String>() {
#Override
protected String doInBackground(Void... params) {
Log.v("TAGGG", "IN doInBackground");
msg = "Error: ";
return msg;
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
Log.v("TAGGG", "Progress: " + progress[0] + "%");
}
#Override
protected void onPostExecute(String msg)
{
Log.v("TAGGG", msg);
}
}.execute();
}
You have to use the publishProgress() method: http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...)
This method can be invoked from doInBackground(Params...) to publish
updates on the UI thread while the background computation is still
running. Each call to this method will trigger the execution of
onProgressUpdate(Progress...) on the UI thread.
onProgressUpdate(Progress...) will note be called if the task has been
canceled.
#Override
protected String doInBackground(Void... params) {
Log.v("TAGGG", "IN doInBackground");
msg = "Error: ";
int i = 0;
while (i <= 50) {
try {
Thread.sleep(50);
publishProgress(i);
i++;
}
catch (Exception e) {
Log.i(TAGGG, e.getMessage());
}
}
return msg;
}
I want to create a progress dialog in android, keep it open for 2 seconds then close it.
Below is the code I have written:
package com.example.proressdialogtest;
import android.app.activity;
import android.app.ProgressDialog;
import android.os.bundle;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ProgressDialog pg = new ProgressDialog(MainActivity.this, 4);
pg.show(MainActivity.this, null, "Searching...", false);
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
pg.dismiss();
}
When I run the code on my device, the ProgressDialog is opened and then it stays open, it does not close after 2 seconds. What am I doing wrong?
As per the answers below. I have added the onPreExecute() and onPostExecute() methods before and after the doInBackground method respectively.
Here is the code for the two methods.
ProgressDialog pd;
public void onPreExceute() {
pd = new ProgressDialog(MainActivity.this);
pd.show(MainActivity.this, "", "Searching...", false);
}
public void onPostExecute() {
pd.dismiss();
}
The problem still persists. The progress bar will not close.
You are calling sleep() on the UI Thread. Don't do this. Use runOnUiThread or an AsyncTask
Painless Threading
AsyncTask
I would use an AsyncTask. You will start the ProgressDialog in onPreExecute() then close it in onPostExecute(). It is a UI element so using it in doInBackground() will give you the error
This SO answer has a good example of using it
Edit:
Use this code
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final ProgressDialog pg ;
pg = ProgressDialog.show(MainActivity.this, null, "Searching...", false);
Thread timer=new Thread(){
public void run()
{
try {
sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
finally{
pg.dismiss();
}
}
};
timer.start();
}
}
You are pausing the UI thread by calling
Thread.sleep(2000);
Because Thread.sleep(x) will make the current thread sleep for x milli seconds
which is bad thing.
place
I don't know why
pg.dismiss() in finally block to make sure that progress dialog will be closed.
And also your Code won't run because you have missed
super.onCreate(savedInstanceState);
following code will work for you,
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog = null;
#Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(MainActivity.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
return false;
}
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
}
}.execute();