MetaIO progressDialog freezes when loading SDK - java

I am quite new to Android/Java, and my first app is using MetaIO SDK.
I am trying to implement "Loading" progress bar, while app (MetaIO SDK) is loading.
Overlay background is shown
Loading dialog is appeared and "loading image" starts spinning
Overlay background disappears and loading image stops spinning <- the problem
After 2-3 seconds it unfreezes and ARELViewActivity is executed.
The code:
public void onScanButtonClick(View v)
{
new ScanLoadingDialog().execute(0);
}
private class ScanLoadingDialog extends AsyncTask<Integer, Integer, Boolean>
{
//Before running code in separate thread
#Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.show();
}
#Override
protected Boolean doInBackground(Integer... params)
{
try
{
synchronized (this) {
AssetsManager.extractAllAssets(getApplicationContext(), true);
startActivity( new Intent(getApplicationContext(), ARELViewActivity.class));
}
}
catch (IOException e)
{
MetaioDebug.log(Log.ERROR, "Error extracting assets: "+e.getMessage());
MetaioDebug.printStackTrace(Log.ERROR, e);
return false;
}
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
finish();
}
}
Am I doing something wrong?
P.S. Full source code can be found here: link text
P.S.S. Related to this question, but I am using technique suggested there, and it still doesn't want to work

I had a similar problem and i solved it by running the UI handling code on the UI thread like so
runOnUiThread(new Runnable() {
#Override
public void run() {
if (imgvExampleOverlay != null)
imgvExampleOverlay.setVisibility(View.VISIBLE);
}
});
imgvExampleOverlay is an image like the one the user has to capture.
Hope this helps

Related

how to keep activity waiting until loading complete android java

I have a splash screen Activity appear for 10 second while waiting this time the Activity check if tables are created and all data are loaded form server
if not it creates tables and load data to DB. every thing is OK but the problem is when loading data take more than 10 seconds the Splash Activity is finished and start another Activity
how i can keep splash activity wait until all data are loaded
here is my code
if(! (checkTables()&&checkData())){
progressDialog.show();
fillSamples();
fillExams();
fillQuestions();
fillSubQuestions();
createProfile();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
Intent studentAccess = new Intent(SplashScreen.this,Samples.class);
startActivity(studentAccess);
finish();
}
},10000);
i am using volley StringRequest and ImageRequest to download data and images from remote server
You can use AsyncTask ,it is better for network calls by creating this inner class
private class Operation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//do what ever operations you want
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "result";
}
#Override
protected void onPostExecute(String result) {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("data", result);
startActivity(i);
finish();
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
and execute the process using private method in splash activity like this
new Operation().execute("");
Network calls should be always called from not main thread.
What you do is that you are making your main thread sleep . Never sleep you main thread. Android will kill your app. Use AsyncTask for network calls (doInBackground) and onPostExectute() do all other things.

Can't see ProgressDialog while AsyncTask run in background

I use AsyncTask in my App for download a url. I use a ProgressDialog on onPreExecute() for waiting.
But I cant see ProgressDialog while process finish and i see it for a moment. want to see it while downloading not after that.
can any one help me.
thanks
my code is like this:
private class loadMoreListView extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
pDialog = new ProgressDialog(SingleMenuItemActivity.this);
pDialog.setMessage("Please Wait ...");
pDialog.isIndeterminate();
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... unused) {
runOnUiThread(new Runnable() {
public void run() {
// do something for downloading
}
});
return (null);
}
protected void onPostExecute(Void unused) {
// closing progress dialog
pDialog.dismiss();
}
}
runOnUiThread(new Runnable() {
public void run() {
// do something for downloading
}
// do something for downloading, inside runOnUiThread, is wrong. runOnUiThread makes "do something for downloading" run on the UI Thread, and your application should crash for NetworkOnMainThreadException, you the app runs on a device with a version of android grater than GingerBread. Differently it will block the ui thread preventing him to draw your progress bar
The problem is in
runOnUiThread(new Runnable() {
public void run() {
// do something for downloading
}
});
ProgressDialog will not update if the UI thread is still busy. There are many examples in SO for that.
I don't understand why do you need UIthread.
And as a rule of thumb - if you need Progress dialog, you need to let run asynctask in background thread,as it always do.Read the document
http://developer.android.com/reference/android/os/AsyncTask.html
You can use the below example
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Firstly notice the "#override" header attached to all the AsyncTask Implemented methods e.g.
private class loadMoreListView extends AsyncTask<Void, Void, Void> {
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(SingleMenuItemActivity.this);
pDialog.setMessage("Please Wait ...");
pDialog.isIndeterminate();
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.cancel();
}
}
Also Remove this from doInBackground unless you must do something on the UI.
runOnUiThread(new Runnable() {
public void run() {
// do something for downloading
}
});
You cannot do something for downloading on the runOnUiThread. doInBackground is meant for running background tasks like downloads etc. not visible to the UI.

Java/Android Asynchronous work in the background

in the past few days I have been trying to figure something but had no luck, I am developing an android game, I have 3 packages for now each with its own purpose:
1 - package for GUI classes.
2 - package that has classes communicates with my wcf service (login/pass DB)
3 - package that holds my asynchronous classes/workers (like a bridge between GUI and SERVICE)
I am not sure if this is even the right approach when it comes to android/java game development, but what I want to achieve is a simple registeration/login in the GUI and when the user is done registering or logining, while the gui talks to the service through the "bridge", a message is displayed for the user like a dialog saying "registering" or "loging in".
Now I would like to hear tips/feedback from more experienced programmers, on how to acomplish this, and if this is the right aproach, and most importantly some examples for this specific case would be really helpfull, I tried to work with the asynctask but I couldn't figure out how to communicate between these 3 seperate packages and return the result from the service back to the gui through the async task.
Take a look at this
public class FindEventsActivity extends Activity {
ProgressDialog pd;
// lots of other code up here
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clickete);
pd = new ProgressDialog(this);
pd.setMessage("loading");
findViewById(R.id.clickLayout).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
new LongOperation().execute("");
pd.show();
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 15; i++) {
try {
Thread.sleep(1000); // Simulates your intensive work
// Update your progress if you want
this.publishProgress();
} catch (InterruptedException e) {
return "Failed";
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// Handle fail or success accordingly
pd.dismiss();
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
// Update UI according to your progress
}
}
}
Hope this helps and enjoy your work

Set background wallpaper from a drawable

In my app, i need to get current wallpaper of the device:
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
no, the problem is that this action will cause the UI to lag a bit while it's getting the background, furthermore, i need to set it as my app's background, i've tried this:
//Drawable Wallpaper defined already...
new Thread(new Runnable() {
public void run() {
Wallpaper = WallpaperManager.getInstance(getApplicationContext()).peekDrawable();
}
}).start();
if (Wallpaper == null)
{
//Resources res = getResources();
//Drawable drawable1 = res.getDrawable(R.drawable.bg1);
//getWindow().setBackgroundDrawable(drawable1);
}
else
{
Wallpaper.setAlpha(50);
getWindow().setBackgroundDrawable(Wallpaper);
}
//........
but it's not working, any ideas?
if possible, please give the code, i'm still kinda new to android..
also, is there a better way to do this?
Try the following and see if it helps. I've commented the code for you to expand on (if needed).
private class SetWallpaperTask extends AsyncTask<Void, Void, Void> {
Drawable wallpaperDrawable;
#Override
protected void onPreExecute() {
// Runs on the UI thread
// Do any pre-executing tasks here, for example display a progress bar
Log.d(TAG, "About to set wallpaper...");
}
#Override
protected Void doInBackground(Void... params) {
// Runs on the background thread
WallpaperManager wallpaperManager = WallpaperManager.getInstance
(getApplicationContext());
wallpaperDrawable = wallpaperManager.getDrawable();
}
#Override
protected void onPostExecute(Void res) {
// Runs on the UI thread
// Here you can perform any post-execute tasks, for example remove the
// progress bar (if you set one).
if (wallpaperDrawable != null) {
wallpaperDrawable.setAlpha(50);
getWindow().setBackgroundDrawable(wallpaperDrawable);
Log.d(TAG, "New wallpaper set");
} else {
Log.d(TAG, "Wallpaper was null");
}
}
}
And to execute this (background) task:
SetWallpaperTask t = new SetWallpaperTask();
t.execute();
If you're still stuck, I recommend you go through the SetWallpaperActivity.java example and try to replicate that.

Can I restrict a ViewFlipper from flipping until an AsyncTask has been performed?

My application has a ViewFlipper with 3 ViewGroups in it. Each ViewGroup interaction is dependent on data from a database. I'm using an AsyncTask to read from a database and return a Cursor when it's done. Before the AsyncTask is executed, I just want to display a single View in the ViewFlipper saying "Loading data, please wait.", is this possible somehow?
Show the progress dialog in your onPreExecute() and dismiss it in the onPostExecute(). Something like this,
private class MyAsyncTask extends AsyncTask<Integer, Integer, Integer[]> {
private ProgressDialog myWait = null;
// This is on the UI thread itself
protected void onPreExecute() {
myWait = new ProgressDialog(MainActivity.this);
myWait.setMessage("Loading data, please wait");
myWait.setCancelable(false);
myWait.show();
}
// Separate worker thread is used here
protected Integer[] doInBackground(Integer...params) {
//do the database loading
return <your result - goes to onPostExecute>;
}
// This is on the UI thread itself
protected void onPostExecute(Integer[] resultCell) {
if (myWait != null) {
myWait.dismiss();
}
}
}
yes you can make use of progressDialog. Do it like this,
progressDiaolg=ProgressDialog.show(Activity.this,"","Loading Images...");
final Thread t= new Thread(new Runnable() {
public void run() {
Log.i("Inside Thread", "Downloading Images...");
downloadImages();
handler.sendEmptyMessage(0);
}
});
t.start();
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
try {
progressDiaolg.dismiss();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
};
I don't have idea with Asynctask. So try modifying this snippet accordingly.

Categories

Resources