I am trying to do some tasks in background with AsynTask. As below:
class CheckTypesTask extends AsyncTask<Void, Void, Void> {
ProgressDialog Asycdialog = new ProgressDialog(Home.this);
String typeStatus;
#Override
protected void onPreExecute() {
super.onPreExecute();
Asycdialog.setMessage("First time initialization. Please wait...");
Asycdialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
create_database_and_tables();
insert_in_questions();
insert_in_answer_option();
return null;
}
#Override
protected void onPostExecute(Void result) {
// hide the dialog
super.onPostExecute(result);
database.close();
Asycdialog.dismiss();
}
Now the problem is that, if i press the hard back key while ProgressDialog is visible, it simply just closes the ProgressDialog. But when i check the Database, i find that all of my records which are to be inserted, are inserted successfully. I dont understand why the ProgressDialog keeps on showing even if the doInBackground() finishes its work, no matter the ProgressDialog disappears after sometime, but why is it so ??
My next question is that, if the data set that is to be inserted is not big enough, then why ProgressDialogstill appears. Actually, i tested like this. I pressed the hard back key immediately after two seconds. And checked the database, I found that all the records were inserted. Why onPostExecute() is not called as soon as the doInBackground() finishes its work ??
And the last question is, i want to show an AlertDialog with yes and no Button, when hard back key is pressed while ProgressDialog is still visible. Then if, yes is pressed the ProgressDialog should disappear and the doInBackground() work should be stopped. And if no is pressed, then only the AlertDialog should be dismissed, and the ProgressDialog should still be visible.
Please help me solving my doubts.
If you place some line of code in doInBackground that only executed once and not in loop,there is no way to cancel it after executing your task.But if you have loop in it or other commands that run periodically,you can use setCancel(true) for your task and check the value of isCanceled() of task before doing your statement in loop.
I guess that when you close dialog,it takes some times to check DB records and during this timedoInBackground continues until all records inserted in DB.So when you check your DB,see all records are inserted.
Also onPostExecute() immediately being invoked on the UI thread after doInBackground(Object[]) returns(if task was not canceled).For example execute this AsyncTask:
class Test extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
System.out.println("End doInBackground");
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
System.out.println("Start onPostExecute");
}
}
But you added some commands before dismissing dialog that may consumes little times and also Activity transitions(dismissing dialog and foregrounding Activity) consumes some times itself.
Edit:
And the last question is, i want to show an AlertDialog with yes and
no Button,...
You have to add a method to your AsyncTask class that shows an alert dialog with desired functionality and store a reference to task and also override onBackPressed() method of Activity.When onBackPressed() executed,call that method of Task instance.Although you know you can not cancel inserting progress in this way.If you want that no record insert to DB,you can remove them in background, after they inserted!!
Related
I'm trying to develop an application to understand android, application delete's default browser's history. Every thing is working fine, I'm using AsyncTask to accomplish the task with ProgressDialog
Here is how I'm deleting the History
ProgressDialog pd;
new AsyncTask<Void, Void, Void>()
{
#Override
protected void onPreExecute()
{
pd = ProgressDialog.show(HistoryClean.this, "Loading..",
"Please Wait", true, false);
}//End of onPreExecute method
#Override
protected Void doInBackground(Void... params)
{
Browser.clearHistory(getContentResolver());
return null;
}//End of doInBackground method
#Override
protected void onPostExecute(Void result)
{
pd.dismiss();
}//End of onPostExecute method
}.execute((Void[]) null);//End of AsyncTask anonymous class
But instead of ProgressDialog I want to implement CircularProgress which it can display the progress value like 10% , 90%....
Some times History may gets deleted faster and some times it may be slow, how to address this problem and dynamically update the CircularProgress bar with Progression Values.
Thanks in advance.
The best two library i found on the net are on github
https://github.com/Todd-Davies/ProgressWheel
https://github.com/f2prateek/progressbutton?source=c
Hope that will help you
AsyncTask has method onProgressUpdate(Progress... values) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress(Progress...).
Refer to the docs for more details
I need to make a transition screen, ou just put a dialog, because the app give a black screen when is creating the database.
I have google, and find some solutions for this. One of then, is just put a progress dialog when the database is been created.
My problem, and newbie question is, where do i put the progress dialog.
A -> BlackScreen -> B where A is the inicial menu, and B the other screen. I have tried to put the dialog on A and/or in B and dont work. So where can i put the code of the progress dialog, so it shows in the BlackScreen ?
Make use of Asyntask . put your database operation of creating database in asyntask in pre execute start dialog post execute cancel dialog in background perform database operation
http://developer.android.com/reference/android/os/AsyncTask.html
For that You have to use Async task :
class DownloadAsyncTask extends AsyncTask<String, String, Void>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(Login.this, "", "Please Wait ...");
}
#Override
protected Void doInBackground(String... arg0) {
//Do your Task
}
#Override
protected void onProgressUpdate(String...values){
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
progressDialog.dismiss();
}
}
//Create the Object
DownloadAsyncTask downloadAsyncTask = new DownloadAsyncTask();
downloadAsyncTask.execute();
now till your work get's completed it shows progress dialog inside the doInbackground write your logic and onPostExecute dismiss the dialog and call Intent of other Activity.
I have a ListActivity which launches another Activity based on the list selection. This second Activity needs to load a fair bit of data from the internet and as such there is a noticeable delay between when the user clicks on an item and when the Activity displays.
This is a problem because I currently have no way to indicate to the user that their click is being processed (even just changing the colour of the selected list item would be sufficient but I can't find a good way to do that). Ideally I'd be able to display an indeterminate ProgressDialog while the second Activity is loading.
I've tried a few different approaches for this but nothing seems to work as desired.
I've tried the following:
Retrieving the serializable data (not all of it but some part) in an AsyncTask in the first Activity and passing it as an extra to the second. This didn't really work well as a ProgressDialog I created in onPreExecute() didn't display immediately (it seems delayed by the processing done in doInBackground() for some reason.)
Here is the code for that:
AsyncTask<String, Void, String> read = new AsyncTask<String, Void, String>() {
Dialog progress;
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(SearchActivity.this,
"Loading data", "Please wait...");
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
DatasetReader reader = new DatasetReader();
reader.setFundID(params[0]);
reader.addDatsets(FundProfile.datasets);
reader.populate();
return reader.toString();
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.dismiss();
}
};
read.execute(selectedItem.getUniqueID());
try {
action = new Intent(SearchActivity.this, FundProfile.class);
action.putExtra("data", read.get());
} catch(Exception ex) {
ex.printStackTrace();
}
In the second Activity's onCreate() method (this does not work at all):
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarVisibility(true);
Here is the onCreate() method for the second approach:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitleColor(Color.WHITE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarVisibility(true);
try {
setContentView(R.layout.fund_profile);
// init some data
setProgressBarVisibility(false);
} catch(Exception ex) {
FundProfile.this.finish();
}
}
If you have long operations you should not be doing them in onCreate in any case as this will freeze the UI (whether or not the activity is displayed). The UI set by onCreate will not appear and the UI will be unresponsive until after the onCreate call finishes.
It seems you can start your second activity and display a progress bar (or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);), then start an ASyncTask which will be responsible for updating your UI once data has been retrieved.
Adam,
It sounds like you are looking for the Indeterminate Progress bar: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ProgressBar2.html
You can display this while you are loading your second Activity then set the visibility to false once the second Activity has loaded its data.
Move creating the Intent -- and really anything you need to do after the AsyncTask completes -- into onPostExecute:
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.dismiss();
Intent action = new Intent(SearchActivity.this, FundProfile.class);
action.putExtra("data", result);
// ... do more here
}
The problem is that AsyncTask.get() blocks until the task is completed. So in the code above, the UI thread is blocked and the ProgressDialog is never given a chance to appear until the task completes.
In my app I am doing some intense work in AsyncTask as suggested by Android tutorials and showing a ProgressDialog in my main my activity:
dialog = ProgressDialog.show(MyActivity.this, "title", "text");
new MyTask().execute(request);
where then later in MyTask I post results back to activity:
class MyTask extends AsyncTask<Request, Void, Result> {
#Override protected Result doInBackground(Request... params) {
// do some intense work here and return result
}
#Override protected void onPostExecute(Result res) {
postResult(res);
}
}
and on result posting, in main activity I hide the dialog:
protected void postResult( Result res ) {
dialog.dismiss();
// do something more here with result...
}
So everything is working fine here, but I would like to somehow to update the progress dialog to able to show the user some real progress instead just of dummy "Please wait..." message. Can I somehow access the progress dialog from MyTask.doInBackground, where all work is done?
As I understand it is running as separate Thread, so I cannot "talk" to main activity from there and that is why I use onPostExecute to push the result back to it. But the problem is that onPostExecute is called only when all work is already done and I would like to update progress the dialog in the middle of doing something.
Any tips how to do this?
AsyncTask has method onProgressUpdate(Integer...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().
Refer to the docs for more details
you can update from AsyncTask's method onProgressUpdate(YOUR_PROGRESS) that can be invoked from doInBackground method by calling publishProgress(YOUR_PROGRESS)
the data type of YOUR_PROGRESS can be defined from AsyncTask<Int, YOUR_PROGRESS_DATA_TYPE, Long>
I've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. My code is as follows:
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected Void doInBackground(Void... unused) {
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
}
Which I call in onCreate() with
new AddTask().execute();
The line items = parser.getItems() works fine - items being the arraylist containing each item from the XML. The problem I'm facing is that on starting the activity, the ProgressDialog which i create in onPreExecute() isn't displayed until after the doInBackground() method has finished. i.e. I get a black screen, a long pause, then a completely populated list with the items in. Why is this happening? Why isn't the UI drawing, the ProgressDialog showing, the parser getting the items and incrementally adding them to the list, then the ProgressDialog dismissing?
I suspect something is blocking your UI thread after you execute the task. For example, I have seen folks do things like this:
MyTask myTask = new MyTask();
TaskParams params = new TaskParams();
myTask.execute(params);
myTask.get(5000, TimeUnit.MILLISECONDS);
The get invocation here is going to block the UI thread (which presumably is spinning off the task here...) which will prevent any UI related stuff in your task's onPreExecute() method until the task actually completes. Whoops! Hope this helps.
This works for me
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(viewContacts.this);
dialog.setMessage(getString(R.string.please_wait_while_loading));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
It is because you used AsyncTask.get() that blocks the UI thread "Waits if necessary for the computation to complete, and then retrieves its result.".
The right way to do it is to pass Activity instance to your AsyncTask by constructor, and finish whatever you want to do in AsyncTask.onPostExecution().
If you subclass the AsyncTask in your actual Activity, you can use the onPostExecute method to assign the result of the background work to a member of your calling class.
The result is passed as a parameter in this method, if specified as the third generic type.
This way, your UI Thread won't be blocked as mentioned above. You have to take care of any subsequent usage of the result outside the subclass though, as the background thread could still be running and your member wouldn't have the new value.