The next version of my app needs to upgrade the database and this takes quite a bit of time. I'd like to show a progressDialog to update the user on the progress. Problem is, I can't quite figure out how and where to create the dialog.
My basic setup is that I have an activity which is essentially a splashscreen. It's on this screen I would like to show the progress. I have a separate DbAdapter.java file where a DatabaseHelper class extends SQLiteOpenHelper, where I override onUpgrade (the upgrade part is working fine).
I've tried a few different places to implement the progress dialog, but I don't seem to find the right spot. I tried passing context from my splashscreen activity to onUpgrade, but when onUpgrade runs it seems to be getting the context from my ContentProvider instead.
Does anyone have a good example of how to display a progress dialog when upgrading a database?
You need to implement an AsyncTask. Example:
class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
//show your dialog here
progressDialog = ProgressDialog.show(this, "title", "message", true, false)
}
#Override
protected Void doInBackground(Void... params) {
//update your DB - it will run in a different thread
return null;
}
#Override
protected void onPostExecute(Void result) {
//hide your dialog here
progressDialog.dismiss();
}
}
Then you just have to call
new YourAsyncTask().execute();
You can read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html
ProgressDialog myProgressDialog = null;
public void DownloadFiles() {
myProgressDialog = ProgressDialog.show(this, "Please wait !",
"Updating...", true);
new Thread() {
public void run() {
try {
//Your upgrade method !
YourUpdateFunction();
} catch (Exception e) {
Log.v(TAG, "Error");
}
myProgressDialog.dismiss();
}
}.start();
}
Related
Found that weird bahaviour of ProgressDialog.
I show ProgressDialog in onClickListener of list before starting new Thread and dismiss it inside that Thread but after all work is done:
GlobalProgressDialog.show(getActivity());
new Thread(new Runnable() {
#Override
public void run() {
//...all other logic
GlobalProgressDialog.dismiss();
}
}).start();
and that GlobalProgressDialog i use to simplify calls:
public class GlobalProgressDialog{
private static ProgressDialog progressDialog;
public static void show(Activity activity){
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
public static void dismiss(){
if (progressDialog != null) {
progressDialog.dismiss();
}
}
}
Dialog appears but NOT exactly after show() being called! I have debugged it and found out that there are 4 standard Android classes are being operated:
AdapterView.java
AbsListView.java
Handler.java
Looper.java
And only after Looper the ProgressDialog is being snown. Is it possible to trick this or fix? Or maybe there's some my fault in code? What could it be?
The problem is that delay between item click and show() is like 1 sec. So application freezes for 1 sec. And only then dialog appears and all work done takes 1-2 sec, sometimes even 0.5 sec. In such cases its not cool to look at frozen app and flashed for 0.5 sec progress dialog.
Thanks in advance.
You're doing too much work on the main thread. show() will show the dialog, but it will do so on the main thread. It does not block execution until it is dismissed. So something you're doing in your code after calling show() and which you are not showing us, is causing the delay in the appearance of the dialog.
Just to be clear again, this has nothing to do with the Thread you're starting and changing it to an AsyncTask won't fix it, although it might be a good idea to do that anyway if the pattern fits your use case.
You should use a AsyncTask here.
public class DialogAsync extends AsyncTask<Void, Void, Void> {
private Context context;
private ProgressDialog progressDialog;
public DialogAsync(Context context) {
this.context = context;
progressDialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Perform your logic here
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}
Usage:
DialogAsync globalProgessDialog = new DialogAsync(getActivity());
globalProgessDialog.execute();
I'm making an application, which only pick some datas on a website. But the problem is that the layout does not show up until the datas are available, the application seem to be blocked while it is looking for the datas. I tried to put the content view in the oncreate and then change the text in the onstart, when I have the datas, but the application still blocks.
Is this possible to print a default text, and then change it when the application have the datas?
Try this, it uses AsyncTask:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
//show loading dialog
progressDialog= ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
//do loading operation here
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
You can call this using from your onCreate():
LoadData task = new LoadData();
task.execute();
Have you tried AsyncTask to download data from website (API)? It will show you progressdialog until data is not downloaded.
So I'm relatively new to Android (and Java). I've made a class which has your usual AsyncTask with a ProgressDialog in a static method because I want to call it from multiple Activities.
public class SomeClass {
// Some other methods, etc.
public static void SomeFunction(final Context context, String FilePath) {
new AsyncTask<Void, Void, Void>() {
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "", "Loading...", true);
}
protected Void doInBackground(Void... unused) {
for (int i=0; i<15000; i++)
System.out.println("Gatorade me, Bitch: " + i);
return null;
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}.execute();
}
}
The problem is that the statement
dialog = ProgressDialog.show(context, "", "Loading...", true);
in the onPreExecute() section of the AsyncTask gives error. The code runs fine without it.
So what can I do to solve this issue without creating a new java class file for AsyncTask. I know that works but I just want to make one file for this entire class so I can use it in multiple programs.
Thanks for your help!
Check whether you are passing application context or activity context while calling the function.
With application context, you cannot show the Progress Bar. If thats the case, try passing activity context.
SomeFunction(ActivityName.this,"path");
I have an AsyncTask that is supposed to show a progress bar while it uploads some stuff via Internet. Sometimes it works like a charm and sometimes it does not show any progress bar. Here is the code:
public class Upload extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(Activity.this);
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
#Override
protected Void doInBackground(Void... params) {
//upload stuff
return null;
}
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing())
dialog.dismiss();
dialog = null;
} catch (Exception e) {
// nothing
}
Intent next = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(next);
}
}
}
The doInBackground and onPostExecute work always, and sometimes altogether it works like a charm. But sometimes, there is no progress bar while it is uploading. Is this a race condition? I do not think so, but I cannot find any explanation.
You're creating the object twice in the class. The ProgressDialog.show already returns a created ProgressDialog object, but you have instantiated it first at the top. The ProgressDialog should be instantiated once, so try removing the instantiation at the top and try again, like so:
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
Maybe it is because void parameter that causes that problem. Just try to use Integer as your parameters.:
public class Upload extends AsyncTask<Integer, Integer, Integer>
I am working on an android app, in that app i have intent2 which on click redirects to intent3 and takes some time then loads a table and displays server data into it.
Sometimes if there is a lot of data, it tales pretty much time to get the dataload and the time blank screen is displayed increases.
i wish to show a loading bar till the data loads.
how can i show the ProgrssBar till only when data is not displayed ?
Probably your best bet would be to use AsyncTask in your "intent3":
You could do it like this:
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(ClassName.this);
protected void onPreExecute()
{
Dialog.setMessage("Please wait...");
Dialog.show();
}
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
#Override
protected Void doInBackground(Void... params)
{
// Do your background data fetching here
return null;
}
}
You probably need to run an AsyncTask on onCreate when you open the new activity, the structure of the asynctask would be like this (taken from the google doc), notice that if you want to increament a progress bar you have to implement onProgressUpdate and call publishProgress in the doInBackground method
private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute()
{
// show your progress bar
}
protected Void doInBackground(Void... params) {
// do your work and publish the progress
publishProgress(progress);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Void result) {
//dismiss your progress bar
}
}
This code is just an example, of course you need to adapt it to your logic/code.
Check out this simple and complete example