Calling a Static Method with AsyncTask/ProgressDialog of a class - android

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");

Related

Android AsyncTask onPreExecute not called indeterminantly

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>

AsyncTask not showing the ProgressDialog

I am using an AsyncTask (which I am starting in my main activity) to load some data:
Context context = VehicleTabView.this;
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
new LoadingVehicles(context, progressDialog).execute(null, null, null);
Here is the AsyncClass:
package com.example.schedule_vehicles;
import com.example.utils.VehicleNames;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
// Showing a ProgressDialog once loading the list of vehicles is completed using an AsyncTask
public class LoadingVehicles extends AsyncTask<Void, Void, Void> {
Context context;
ProgressDialog progressDialog;
public LoadingVehicles(Context context, ProgressDialog progressDialog) {
this.context = context;
this.progressDialog = progressDialog;
}
#Override
protected void onPreExecute() {
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
new VehicleNames(context);
return null;
}
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}
The problem that the ProgressDialog is not showing on the screen.
I type Log.d, to see if the program is going through all the phases - onPreExecute, doInBackground, onPostExecute, and it is going through all the phases and doing the job that I need. But the ProgressDialog is not showing. I read a lot of information about this thing and it seems that the PRE and POST execute are started by the main thread, which is blocked by the DOINBACKGROUND method, and this is the reason not to see the ProgressDialog. I tried to find some answer how this is solved - but no success.
If anyone faced this, please share your experience. THANKS a lot!
You're passing the ProgressDialog to the Task, just show() it before you start the AsyncTask, not from within the AsyncTask.
Your code looks good to me. You are correct about your understanding of Asynctask and your use of them also appears correct.
The only thing that I can think of is that you must make sure that you are calling execute() on the UI Thread as well. From the code posted I'm not able to tell what context you are in.
Make sure you can pass in "this" as a context. That will tell you if your on the UI thread or not.
ProgressDialog progressDialog = new ProgressDialog(this);
Try :
ProgressDialog progressDialog = new ProgressDialog([Activity Name].this);
Let me know if this solves the problem or I'll see in depth.
Maybe you are missing the context.
ProgressDialog progressDialog = new ProgressDialog(this);
Normally when creating a ProgressDialog, you use the static method ProgressDialog.show(context, title, message). This will create and show the message and give you back a reference to the dialog.
onPreExecute and onPostExecute are called on the main thread, and are not blocked by the doInBackground, which runs on another thread. onPreExecute is called before doInBackground and onPostExecute is called after.
Here's some example code:
public static class InitializeTask extends MyAsyncTask<String, String, Response<Object>> {
private Activity activity;
private ProgressDialog dialog;
public InitializeTask(Activity activity) {
this.activity = activity;
}
#Override
protected void onPreExecute() {
dialog = ProgressDialog.show(activity, null, "Initializing...");
}
#Override
protected void onPostExecute(Response<Object> result) {
if (dialog != null && dialog.isShowing())
dialog.dismiss();
}
#Override
protected Response<Object> doInBackground(String... params) {
}
#Override
protected void attach(Activity context) {
this.activity = context;
dialog = ProgressDialog.show(context, null, "Initialize...");
}
#Override
protected void detach() {
if (dialog.isShowing())
dialog.dismiss();
activity = null;
}
}
Attach and detach are my own methods for referencing a cross orientation changes.

How to perform commands after "setContentView" is being called

I'm using "include" on my main layout. Each one of them is a RelativeLayout which needs an OnClick listener to be attached, and update some information related.
So I've tried to do it simply by:
setContentView(R.layout.allobjects);
ObjectListeners objectListeners = new ObjectListeners(objects);
for(int i=0;i<1;i++)
{
RelativeLayout objectBoxRelativeLayout = (RelativeLayout)findViewById(R.id.object1 + i);
objectBoxRelativeLayout.setOnClickListener(objectListeners.GetObjectListener(i));
SomeObject currentObject = this.objects.get(i);
Object viewObject = findViewById(R.id.object1 + i);
this.setObjectView(viewObject, currentObject);
}
The issue is that it takes too long after the "setContentView(R.layout.allobjects);" command, and the application shows black screen until it finish loading.
In addition, I use "setContentView(R.layout.allobjects);" after I perform the above commands. All of these commands have to be written after "setContentView(R.layout.allobjects);".
How can I handle that kind of situation ? Do I have to use onPreExecute and implement AsyncTask ?
Yes, AsyncTask is good solution to show loading dialog while these commands being executed.
UPDATE:
Add this class under your onCreate() function:
private class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
private Context context;
public MyTask(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
dialog.setTitle("Loading...");
dialog.setMessage("Loading...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
//do your code here in background
protected void onPostExecute(Void res) {
dialog.dismiss();
}
}
then use the task inside onCreate() like this:
MyTask mt = new MyTask(this);
mt.execute();

ProgressDialog is not shown in the AsyncTask

Document doc = new Obtainer(context, uri).execute().get();
This code in the activity class renders the Obtainer(which extends AsyncTask) which gets the xml document from the url. This is the onPreExecute method:
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Pre execute began");
exception = null;
dialog = new ProgressDialog(context);
dialog.setMessage("Loading started");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
System.out.println("Preexecute end");
dialog.show();
}
context is set in the Constructor:
public Obtainer(Context c, String addr) {
context = c;
address = addr;
}
During the runtime I can see in the console output both "Pre execute began" and "Preexecute end" but the progress dialog is not shown. What is the probleM?
Use this code, it works for me:
class Obtainer extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(App.this); // App - your main activity class
dialog.setMessage("Please, wait...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// ...
}
#Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
And in your main activity class method call
new Obtainer().execute();
What Context are you passing when you create your Obtainer (AsyncTask subclass)?
If you are using the Application context via getApplicationContext(), it can not be used to create a Dialog (or any View for that matter). You need to pass it a Context that can create Views.
"If you're in the habit of using your application context (from a call to getApplicationContext(), for example) in places where you need a Context to create views, it's only a matter of time until you find a case where things don't work quite like you would want or expect."
From: https://plus.google.com/107708120842840792570/posts/VTeRBsAeyTi

Displaying a progress dialog when upgrading database

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();
}

Categories

Resources