I have a class in which i have to execute 3 long operations .And have to show the progress bar at the intervals 25%,50%,75% and 100%.
Since the operations use the UI thread , i cannot place them in the DoinBackground method()..
I am placing the opeartions in progressUpdate method
Code
package com.integrated.mpr;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.correlation.Covariance;
import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.util.FastMath;
public class Logic extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
String x = "abc";
new loadSomeStuff().execute(x);
}
public class loadSomeStuff extends AsyncTask<String,Integer,String>{
ProgressDialog dialog;
protected void onPreExecute(){
dialog = new ProgressDialog(Logic.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress(25);
publishProgress(50);
publishProgress(75);
publishProgress(100);
return null;
}
protected void onProgressUpdate(Integer...progress){
if(progress[0]==25){
dialog.incrementProgressBy(25);
Log.d("now in ", "25 loop");
// do some long work in loop1
dialog.incrementProgressBy(25);
}
else if(progress[0]==25){
dialog.incrementProgressBy(25);
Log.d("now in ", "50 loop");
// do some long work in loop2
dialog.incrementProgressBy(25);
}
else if (progress[0] == 75){
dialog.incrementProgressBy(25);
// do some long work in loop3
}
else{
dialog.incrementProgressBy(25);
}
}
protected void onPostExecute(String result){
dialog.dismiss();
Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
startActivity(openList);
}
}
}
Now , when i run this a blank screen appears , and the progress displays just for 100% .
How can i correct this?? please help
First of all you are making some BIG error.
onProgressUpdate is used to update the UI so you have not to do some long work on that method. All the long work you have to made it in doInBackground and when you finish a part of it you can call the onProgressUpdate to update the UI.
Also
Now , when i run this a blank screen appears , and the progress
displays just for 100%
That's because you are doing no work and your phone is fast :P
Try to put a Thread.sleep(1000) betweek each call of onProgressUpdate :)
Than you will replace the sleep with real work!
You should not use the ui thread to compute long operation. Your long operations should be executed in doinbackground which will execute them in a different thread.
Edit : I think the problem is that you really want to work with the ui thread for whatever reason you have. If that's the case, maybe a handler would be more suitable.
http://developer.android.com/reference/android/os/Handler.html
Related
I am writing a Android application which reads data from a SQLite Database and then displays the data on a next screen. Whenever I was doing a query on the database I would get an error message that too much work is being done on the main thread.
I then put my query in a new Thread:
(new Thread()
{
public void run()
{
Looper.prepare();
try
{
FPJobCardWizard data = dbHelperInstance.loadFPJobCardWizardFull(fitmentHash);
wState.fitmentItemSet(data.fitmentItemGet());
} catch (Exception e) {e.printStackTrace();}
Looper.loop();
}
}).start();
Now the gui/main thread is completing it's operation prior to the Query being complete and as a result the data variable is still empty. I read a few posts and the API documentation and it seems that I need to use a Looper (this seems to be the correct fix) but I have never used a Looper and cannot seem to get it to work.
Please can you check the code above and guide me in the right direction.
Thank you all in advance.
the best choice here will be using an AsyncTask, as it will enables you to perform all the background work in a background thread, then when the result is generated it will apply it using the UI thread:
So, as explained in the life cycle of AsyncTask, you can do all of your background work in the method doInBackground() and then do all of your UI work on the method onPostExecute() which will be executed after taking the result from doInBackground() method according to the life cycle, and to put your hands more on the AsyncTask, have a look at this example which provides the following example code:
public class AsyncTaskTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// This starts the AsyncTask
// Doesn't need to be in onCreate()
new MyTask().execute("my string paramater");
}
// Here is the AsyncTask class:
//
// AsyncTask<Params, Progress, Result>.
// Params – the type (Object/primitive) you pass to the AsyncTask from .execute()
// Progress – the type that gets passed to onProgressUpdate()
// Result – the type returns from doInBackground()
// Any of them can be String, Integer, Void, etc.
private class MyTask extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
#Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
#Override
protected String doInBackground(String... params) {
// get the string from params, which is an array
String myString = params[0];
// Do something that takes a long time, for example:
for (int i = 0; i <= 100; i++) {
// Do things
// Call this to update your progress
publishProgress(i);
}
return "this string is passed to onPostExecute";
}
// This is called from background thread but runs in UI
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
}
}
}
Goal: Have a ProgressDialog which shows "Loading..." until next Activity is completely loaded and shown on screen.
Tried with ProgressDialog context and activity set to originating Activity. Also tried with getApplicationContext() and getParentContext(). Exceptions on the last two approaches. Need to do this as destination Activity is slow to render due to non-simple Layout file. (Cannot fix that right now due to organizational issues.) Turns out the destination Activity takes 1-2 seconds to OnCreate and then screen goes black for up to 5+ seconds then it paints. The rendering is just slow. Did review with Hierarchy Viewer and see lots of red balls but can't fix now.
Read up on some related but haven't found a fix. E.g. What's the difference between the various methods to get a Context?
E.g. both of these crash. Using the "this" of source Activity doesn't work either.
// Context parentContext = this.getParent().getBaseContext();
Context parentContext = this.getApplicationContext();
ProgressDialogMenuable theProgressDialog = new ProgressDialogMenuable(parentContext,this);
theProgressDialog.setTitle("yeeha");
theProgressDialog.setMessage("weewah");
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(true);
theProgressDialog.show();
Also, oddly, nothing happens when I do this:
theProgressDialog.show();
ActivityHelper.changeActivity(this, v, InsMyHoldingsActivity.class, extraMap, -1, -1);
User clicks button to show next activity but the ProgressDialog conflicts with the Activity launch and nothing actually happens other than the button becoming yellow ontouch. Button below works. removing ProgressDialog creation and it works. No console messages logged. A little offputting to the developer for sure.
You can show a progress dialog like this -
Define this
private ProgressDialog pd = null;
in your activity class
Put this in your onCreate (Dont setContentView directly here)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.pd = ProgressDialog.show(this, "Fancy App",
"Loading...Please wait...", true, false);
// Start a new thread that will download all the data
new IAmABackgroundTask().execute();
}
// Background heavy lifting
class IAmABackgroundTask extends
AsyncTask<String, Integer, Boolean> {
#Override
protected void onPreExecute() {
// showDialog(AUTHORIZING_DIALOG);
}
#Override
protected void onPostExecute(Boolean result) {
// Pass the result data back to the main activity
ActivityName.this.data = result;
if (ActivityName.this.pd != null) {
ActivityName.this.pd.dismiss();
}
setContentView(R.layout.main);
}
#Override
protected Boolean doInBackground(String... params) {
//Do all your slow tasks here but dont set anything on UI
//ALL ui activities on the main thread
return true;
}
}
Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance.
Also Use Traceview to look for bottlenecks
There is two ways to
First approach To use Async Task
If you are doing heavy tasks eg loading data from server or parsing xml in that case use AsynTask<> If you want to call ActivityB from ActivityA then
*step-1*create a AsyncTask class. write all background tasks inside doBackground() method and after completion of task you want to call an activity that code write inside onPostExecute() post execute method
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
public class LoadingDataFromServer extends AsyncTask {
Context currentContext = null;
boolean isCancelled = false;
public LoadingDataFromServer(Context context) {
currentContext = context;
}
#Override
protected void onPreExecute() {
if (DashboardActivity.progressBarLayout != null) {
DashboardActivity.progressBarLayout.setVisibility(View.VISIBLE);
// Log.i(TAG,".....Now make progress bar visible.....");
}
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
// do background processing
try {
// do background tasks eg sever communication
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// progressDialog.dismiss();
// call second Activity
Intent i = new Intent(currentContext, com.ActvityB.class);
super.onPostExecute(result);
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
isCancelled = true;
super.onCancelled();
}
}
step-2 In the activity fro where you want to jump to new activity (eg in ActivityA) call the execute() of AsynTask
new LoadingDataFromServer(context).execute(null);
Second approach
First show progress dialog.
create a thread to do all background tasks. when the thread completes the task then cancel the progress dialog and call the next activity
or
when thread complets the task then call next activity pass this object (progress dialog) and inside that new activity dismiss this dialog.
Here is my code that can help.
In this, I'm only posting the first method of AsyncTask i.e onPreExecute.
User_AsyncTask extends AsyncTask:
public class User_AsyncTask extends AsyncTask<String, String, String>
{
String response = "";
#Override
protected void onPreExecute()
{
try
{
if (progressDialog != null)
progressDialog.cancel();
}
catch (Exception e)
{
}
progressDialog = ProgressDialog.show(DisplayDetails.this, "", "Please wait...", true, true);
progressDialog.setCancelable(false);
progressDialog.show();
}
I'm having a minor problem and I don't know how to do it.
I created a app, that pulls information through an xml web service.
The data get's parsed and inserted into a local sqlite database.
Everything works so far.
Due to the size of the data (that varies based on the pulled information) I intended to insert a loading screen with a progressbar and a spinner to notify the user that actions are taken.
Well let's say it, that it is planed to work this way.
My Problem is, that it is displayed, as soon as the work is done, not bevor.
This is how I've done it:
Handler phHandle = new Handler(){
#Override
public void handleMessage(Message msg){
int progress = msg.arg1;
pb.setProgress(progress);
}
};
class firstInitHandler implements Runnable{
private Handler phHandle;
public void setHandle(Handler h){
this.phHandle = h;
}
public void run(){
try {
firstInit i = new firstInit();
i.doInit(this.phHandle);
if(i.isResult())
i.doParse();
if(i.isData()){
i.doTasks();
i.doEquipment();
i.doUser();
i.doCompany();
i.doForms();
i.doSysconfig();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
firstInitHandler h = new firstInitHandler();
h.setHandle(phHandle);
h.run();
This is part of a method.
Well it works so far.
The thread for processing the information is visible in task manager in the sdk.
I can also see the status messages on logcat that are created while the debug system works.
But the screen stays blank and only is displayed as soon as the task is done.
I'm a little bit confused and don't know where to start to have the waiting screen work.
I would welcome any help.
Chris
You can use AsyncTask fot this to work smoothly....
Inside your activity define the AsyncTask ....
then you can call the AsyncTask from onclick or inside onCreate in this way
new loading().execute();
The AsyncTask class will be as below.
The basic concept is to initialize the progress dialog in
onPerExecute().
Do the heavy stuff (non UI ) like parsing and all in
DoInBackground().
Then dismiss the dialog in onPostExecute().
you cannot do the UI changes in DoinBackGround, but you can do that
in OnPre / OnPostExecute().
the class is as follows.
private class loading extends AsyncTask'<'String, Integer, Boolean>
{
ProgressDialog dialog ;
#Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result==true)
{
dialog.dismiss();
}
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(current.this);
dialog.setMessage("Loading...");
dialog.show();
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
#Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
try {
firstInit i = new firstInit();
i.doInit(this.phHandle);
if(i.isResult())
i.doParse();
if(i.isData()){
i.doTasks();
i.doEquipment();
i.doUser();
i.doCompany();
i.doForms();
i.doSysconfig();
}
} catch (NullPointerException e)
{
e.printStackTrace();
return false;
}
return true;
}
}
Have you tried looking at the Android Developers resources?
Here is the Dialog tutorial. A progress dialog example is shown on this page, or you could hop to the specific progress dialog section where it shows all the possible commands, usages, etc.
Hope this is what you need.
I am on a dilemma on how to achieve "Creating a loading screen" on a AsyncTask mainly using progressDialog.
What I am doing is that I have a method which takes no inputs and just shows a textview.
private class DownloadFilesTask extends AsyncTask {
So what my method does is simply like 1 + 1 = 2 but in a more complex way of storing a giganormous string which then get shown on a textview.
So my question is how do I achieve the result of a progress dialog before this long load?
Android already has a progress dialog class built-in. I'd recommend using that, unless there's something about it that doesn't suit your needs.
private progressDialog pd;
public void runMethod(){
new doStuuff.execute();
}
private Class doStuff extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
//Everything in your code except for anything that is xml or relates with any view. Mostly calculations
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
//Your main background view code ends up falling here
pd.dismiss();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
//Your optional view and where it starts
pd = ProgressDialog.show(View_Book_As_Whole.this, "Loading", "Please wait...");
}
}
I was being too noob not to understand the concept but it is relatively easy once you understand it. onPreExecute() means that it runs before anything and it takes priority first then goes to doInBackground(), and then onPostExecute() it runs the final task. This all works in the background of the UI.
I have an activity 'Activity1' and also another activity named 'Activity2'. The 'Activity2' is loaded upon clicking a button in 'Activity1'. I wanted to display the progress dialog until the new activity is loaded . Can you please show me the code to do this
Display the progress dialog in Activity2's onCreate method, then do all the time-consuming loading in an AsyncTask. In the AsyncTask's onPostExecute() method, dismiss the progress dialog.
There is two ways to
First approach To use Async Task
If you are doing heavy tasks eg loading data from server or parsing xml in that case use AsynTask<>
If you want to call ActivityB from ActivityA then
*step-1*create a AsyncTask class. write all background tasks inside doBackground() method and after completion of task you want to call an activity that code write inside onPostExecute() post execute method
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
public class LoadingDataFromServer extends AsyncTask {
Context currentContext = null;
boolean isCancelled = false;
public LoadingDataFromServer(Context context) {
currentContext = context;
}
#Override
protected void onPreExecute() {
if (DashboardActivity.progressBarLayout != null) {
DashboardActivity.progressBarLayout.setVisibility(View.VISIBLE);
// Log.i(TAG,".....Now make progress bar visible.....");
}
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
// do background processing
try {
// do background tasks eg sever communication
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// progressDialog.dismiss();
// call second Activity
Intent i = new Intent(currentContext, com.ActvityB.class);
super.onPostExecute(result);
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
isCancelled = true;
super.onCancelled();
}
}
step-2 In the activity fro where you want to jump to new activity (eg in ActivityA) call the execute() of AsynTask
new LoadingDataFromServer(context).execute(null);
Second approach
First show progress dialog.
create a thread to do all background tasks. when the thread completes
the task then cancel the progress dialog and call the next activity
or
when thread complets the task then call next activity pass this
object (progress dialog) and inside that new activity dismiss this
dialog.
yes by using AsynTask<> you can get your result
in OnPreExecute Show your Progress dialog,in OndoInBackground run your activity,in onPostExecute remove your dialog
get the idea Get Concept
The other answers (using AsynTask) are correct, but the question you need to figure out is what is causing your delay. Is it something happening on the back end of Activity1 or something happening on the front end Activity2. If you're doing some processing before starting Activity2 then follow the advice of Last Warrior or Ted Hopp... if you have some lengthy loading process in Activity2 then you'll need to initiate the progress dialog as the first thing that happens onCreate of Activity2 and then move whatever is taking up processing resources off into an AsynTask (or just another thread) there.
I guess in the unlikely event that both A1 and A2 are requiring extra time on the end and front of each respectively, you'll need to open and close a progress dialog... I don't think there's a way to keep one open in the foreground as you move from one activity to the other.
you can do it through AsyncTAsk. Which code taking time for executing just put that into
doInBackground()
override method of asyncTask and
startActivity(intent) ----just put into onPostExcute()
protected class InitTask extends AsyncTask<Context, Integer, Integer> {
#Override protected Integer onPreExcute( Context... params ) {
//assign progressbar here
}
#Override protected Integer doInBackground( Context... params ) {
//do all the stuffs here
return super.doInBackground( params )
} #Override protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//update progress bar
}
#Override protected void onPostExecute( Integer result ) {
super.onPostExecute(result);
//start activity here
}
}