AsyncTask not showing dialog during operation - java

The progressDialog of my AsyncTask not showing. It seems correct but it doesn't display the dialog.. The operations inside works perfectly but seems ignoring the onPreExecute() and onPostExecute() methods..
private class copyApk extends AsyncTask<Void, Void, Void> {
int appPosition;
ProgressDialog mProgressDialog = new ProgressDialog(getActivity());
#Override
protected void onPreExecute(Void pre) {
super.onPreExecute();
mProgressDialog.setTitle("Copy apk");
mProgressDialog.setMessage("Copying...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... apks) {
final File customfolder = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"HazyApkBackup");
if(!customfolder.exists()){
customfolder.mkdirs();
}
try
{
vacca = getActivity().getPackageManager().getApplicationInfo(app.getPackageName(), packageManager.GET_META_DATA).sourceDir.toString();
//Toast.makeText(getActivity(), "Boh "+vacca, Toast.LENGTH_SHORT).show();
process = Runtime.getRuntime().exec("cp " + vacca + " " + customfolder);
//Toast.makeText(getActivity(), "Apk copied in "+customfolder, Toast.LENGTH_SHORT).show();
}
catch (PackageManager.NameNotFoundException | IOException e)
{
Toast.makeText(getActivity(), "Sorry, the apk was not copied correctly", Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(mProgressDialog.isShowing())
Toast.makeText(getActivity(), "Apk copied in "+customfolder, Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
}
Thanks

Your onPreExecute() method isn't getting called, as it has the incorrect signature. The correct method has no parameters.
#Override
protected void onPreExecute()
{
...

The prototype of onPreExecute() :
#Override
protected void onPreExecute() {
}
The onPreExecute() and onPostExecute() methods are called on the UI thread. It is always better to use a WeakReference to avoid exceptions in future.
In your AsyncTask, create a WeakReference like :
private WeakReference<MyActivity> myWeakContext;
Then in your onPreExecute(),
MyActivity activity = this.myWeakContext.get();
ProgressDialog mProgressDialog = new ProgressDialog(activity);

Related

How to change progressDialog text

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

Java AsyncTask not showing progress on UI though publishProgress(progress)

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.

ProgressBar not showing up during a AsyncTask

I am trying to make a simple task in background and show a progress bar while it is being done.
This is the code for the main (and the only) Activity:
public class Login extends Activity {
public static ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Sending data...");
progressDialog.setCancelable(false);
(...)
// In some onClick Eevent..
JSONObject result = new Urltasks().execute(...).get();
(...)
}
}
This is the code for the activity:
class Urltasks extends AsyncTask<String, Integer, JSONObject>{
protected void onPreExecute() {
System.out.println("Inicia onPreExecute");
Login.progressDialog.show();
}
protected void onPostExecute(String result) {
Login.progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Some work being done. I do not use Login.progressDialog here
}
}
With this code the ProgressDialog shows up when the task ends, and it doesn't dismiss.
Problem is here:
JSONObject result = new Urltasks().execute(...).get();
Calling get() on an AsyncTask blocks the current thread, i.e. your UI thread, until the AsyncTask is executed. Therefore the progress dialog cannot run. Remove the get().
To obtain the result of the AsyncTask, you can e.g. pass in a listener callback to the asynctask that gets notified when the result is available:
class YourAsyncTask extends AsyncTask<ParamType, ProgressType, ResultType> {
private YourResultListener mListener;
interface YourResultListener {
void onResultAvailable(ResultType result);
}
YourAsyncTask(YourResultListener listener) {
mListener = listener;
}
#Override protected ResultType doInBackground(ParamType... params) {
//...
}
#Override protected void onPostExecute(ResultType result) {
mListener.onResultAvailable(result);
}
}
You can use it like:
mProgressDialog.show();
new YourAsyncTask(new YourResultListener() {
#Override void onResultAvailable(ResultType result) {
mProgressDialog.dismiss();
// use result
}).execute(params);
Personally I like to keep user interface elements such as progress dialogs decoupled from async tasks.
Don't put your progress dialog in your log in class. Keep it in the async task
this is how you show your dialog
progressDialog = ProgressDialog.show(yourActivityHERE.this, "",
"Loading... please wait.");
Edited this for you:
class Urls extends AsyncTask<String, Integer, JSONObject>{
ProgressDialog progressDialog;
protected void onPreExecute() {
progressDialog = ProgressDialog.show(yourActivityHERE.this, "",
"Loading... please wait.");
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Some work being done. I do not use Login.progressDialog here
}
}
create the constructor in your Urltasks sending the context of the calling class.
then use that context to create the progress dialog in the preExecute of your Urltasks
class Urltasks extends AsyncTask<String, Integer, JSONObject>{
Context mContext;
public static ProgressDialog progressDialog;
public UrlTasks(Context c){
mContext=c;
}
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Sending data...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Do your work here
}
}

Trying to display a ProgressDialog in AsyncTask executed in a MapActivity

I have a MapActivity that has code similar to this in onCreate():
if(...) {
...
new FirstAsyncTask(id, this).execute((Object[]) null);
...
} else {
...
myLocationOverlay.runOnFirstFix(new Runnable() {
new SecondAsyncTask(lat, lon, ActivityName.this).execute((Object[]) null);
}
...
}
The FirstAsyncTask and SecondAsyncTask both do different things, but both of them show a ProgressDialog like so:
public FirstAsyncTask(long id, Context context) {
progressDialog = new ProgressDialog(context);
}
protected void onPreExecute() {
...
progressDialog.show();
}
protected String doInBackground(Object... params) {
...
progressDialog.dismiss();
...
}
This is working with FirstAsyncTask, but no matter what I change in the call to SecondAsyncTask it always fails with this error: Can't create handler inside thread that has not called Looper.prepare(). I have tried setting the context parameter to "this", "ActivityName.this", getApplicationContext(), and getBaseContext().
I'm still pretty new to Android so this idea of a "context" is confusing me. I'm even more confused that FirstAsyncTask works but SecondAsyncTask doesn't. I've seen this error mentioned a lot in other questions but none of the answers seem to work. Any ideas?
EDIT: The exception is being thrown when the ProgressDialog is being initialized in the SecondAsyncTask's constructor.
The problem is here
myLocationOverlay.runOnFirstFix(new Runnable() {
new SecondAsyncTask(lat, lon, ActivityName.this).execute((Object[]) null);
}
SecondAsyncTask is being constructed on a newly spawned thread. not the UI thread.
Create the one progressDialog in the activity. And then access the Activity's progressDialog from the Asynctasks.
public class MyActivity extends Activity {
ProgressDialog mProgressDialog;
onCreate(...){
mProgressDialog = new ProgressDialog();
}
private class MyAsyncTask extends AsyncTask<...> {
...
onPreExecute(...){
mProgressDialog.show();
}
onPostExecute(...){
mProgressDialog.dismiss();
}
}
private class MyAsyncTask2 extends AsyncTask<...> {
...
onPreExecute(...){
mProgressDialog.show();
}
onPostExecute(...){
mProgressDialog.dismiss();
}
}
Do not attempt to perform progressDialog.dismiss() from the doInBackground, instead put it in postExecute which is running in the UI thread.
You'll want to set up flags once all this is working so that you only dismiss the progressdialog once both tasks are complete.
Write your dialogue dismiss code inside onPostExecute() method it will run the code in the UI thread
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
Write your AsyncTask class this way:
private class FirstAsyncTask extends AsyncTask<String, Void, Void> {
ProgressDialog myDialog = null;
#Override
protected void onPreExecute() {
myDialog = ProgressDialog.show(YourActivityClass.this, "",
"Loading Data...");
return;
}
#Override
protected Void doInBackground(String... urls) {
//Your code which you want to run in background
return null;
}
#Override
protected void onPostExecute(Void result) {
myDialog.dismiss();
return;
}
}
above code works if you have defined the AsyncTask class as inner class of the Activity. If your AsyncTask class is defined as a seperate class then you have to pass the context of the activity to its constructor
ProgressDialog myDialog = null;
Context context;
public FirstAsyncTask (Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
myDialog = ProgressDialog.show(context, "",
"Loading Data...");
return;
}

Android:problem in displaying the progressbar dialog?

in my app i trying to display a progressbar for a time taken for a function to complete its execution and dismiss it after the function has completed its execution ...
my code definition is as follows :
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROCESS: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Indeterminate");
dialog.setMessage("Please wait while loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
showDialog(PROCESS);
doFunction();
dismissDialog(PROCESS);
and for some reason the processdialog is not is not displayed...
can some one help me out here pls....
is there any other way to do it ...
thanks :)
Since your function takes some time to process, use an async task
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute()
{
pd = ProgressDialog.show(getApplicationContext(),"", "Sending Image ...", true);
Log.d(TAG, "onPreExecute()");
}
#Override
protected Void doInBackground(Void... params)
{
doFunction();
return null;
}
#Override
protected void onPostExecute(Void res)
{
Log.d(TAG, "onPostExecute()");
pd.dismiss();
}
}.execute();
Secondly setting it to indeterminate means you will only see a circle thingy.
You should set the progress style to STYLE_HORIZONTAL

Categories

Resources