Why the dialog don't works in parallel with a thread? - android

Why the dialog don't works in parallel with a thread?
Using this code, the activity freeze and the progress dialog don't show...
I need to show the progress dialog during the download of files...
in the onCreate:
pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.setTitle(null);
pDialog.setMessage(getString(R.string.loading));
in the download method:
startReader = true;
pDialog.show();
new Thread(new Runnable(){
public void run(){
for(int i = 1; i <= Integer.parseInt(pages); i++){
try{
if(!isCached(code,i)){
try{
CODE TO DOWNLOAD THE FILE;
Log.d(TAG, "File downloaded: /"+ code + "/" + "pg" + i + ".rsc");
}catch(IOException e){
runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), getString(R.string.reader_errinternetcon), Toast.LENGTH_SHORT).show();
}
});
}
}
}catch(Exception e){
runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
startReader = false;
break;
}
}
if(startReader){
runOnUiThread(new Runnable(){
public void run(){
Intent intent = new Intent(MainActivity.this, ReaderActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("Pages", pages);
intent.putExtra("Code", code);
getApplicationContext().startActivity(intent);
}
});
}
}
}).start();
pDialog.dismiss();

Thread.start() starts the thread but does not wait for it to finish. You dismiss your dialog immediately afterwards. That's why you don't see the progress dialog.
I suggest you make your background thread an AsyncTask. Set up your progress dialog in onPreExecute(), do your background thread processing in doInBackground() and do UI thread post-processing such as dismissing progress dialogs in onPostExecute().

Related

Setting text on ProgressDialog crashes app

I'm learning android and I don't know why this code doesn't work. Can you tell me why it doesn't work and take me correct code?
final ProgressDialog dialog = ProgressDialog.show(LoginScreen.this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
try
{
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
loggingStatus.start();
You have to move the portion of the background task that updates the ui onto the main thread. There is a simple piece of code for this:
putting runOnUiThread( new Runnable(){ .. inside run():
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
};
loggingStatus.start();
You should not set the Text of ProgressDialog in background thread . All UI updation should be done only on UI thread(Main thread)
Either use AsyncTask or Handler for this logic or functionality

Android window WindowLeaked - Activity has leaked window that was originally added here

im try to show ProgressDialog in side the thread.but when the app run Progressdialog will crach and it give this Exception
android.view.WindowLeaked:
Activity com.testApp.CaptureSignature has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView{528dd504
V.E..... R.....I. 0,0-949,480} that was originally added here
error getting when line executing
pDialog.show();
public void syncing(final int sel){
if(sel==1){
ProgressDialo pDialog = new ProgressDialog(CaptureSignature.this);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.setProgress(0);
pDialog.setOnDismissListener(new MyCustomDialog.OnDismissListener() {
#Override
public void onDismiss(final DialogInterface dialog) {
doaftersync(pDialog.getProgress(),sel);
}
});
pDialog.setMessage("Syncing Deliveries.Please wait..");
pDialog.show();
Thread background = new Thread (new Runnable() {
public void run() {
progressHandler.sendMessage(progressHandler.obtainMessage());
int stat = deliveryup();
if(stat==1){
try {
locationManager.removeUpdates(locationListner);
} catch (Exception e2) {
}
}else{
pDialog.dismiss();
return;
}
progressHandler.sendMessage(progressHandler.obtainMessage());
int isustat=issueup();
if(isustat==0){
pDialog.dismiss();
return;
}
progressHandler.sendMessage(progressHandler.obtainMessage());
int locstat=locationup();
if(locstat==0){
pDialog.dismiss();
return;
}
cleanup();
progressHandler.sendMessage(progressHandler.obtainMessage());
pDialog.dismiss();
return;
}
});
background.start();
}
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
pDialog.incrementProgressBy(25);
}
};
Any Help .. !!
Dismiss Your ProgressDialog in Main Thread Using Handler or Using runOnUiThread() Method
Maybe You get exception because Progress dialog is running while Your Activity is destroyed. you should dismiss dialog when Activity is destroyed
Do all your UI actions in a UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
pDialog.dismiss();
}
});
I think the safe way :
if(dialog.isShowing()){
dialog.dismiss();
}

Only the original thread that created a view hierarchy can touch its views

Im Adding Progress Dialog in some Activity .But im getting Exception mention in title.how to resolve it.
dialog = ProgressDialog.show(Notification.this, "loading please wait",
"Loading. Please wait...", true);
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
dialog.dismiss();
}
}.start();
Any thing wrong with this.all Background process is performed in performbackgroundprocess method.
You cant call dialog.dismiss(); in the background thread.
You can make Threads send messages to handlers when they are done and in the handler you can dismiss the dialog. Handlers work in ui thread
There is a tutorial about it
use runOnUiThread as:
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
CurrentActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}.start();

show and dismiss progress dialog while running a thread in android

I have a method in my activity to download a set of files. This downloading is taking place when I start a new activity. I have used threads, because it downloads completely whereas AsyncTask may sometimes fail to download all files, it may get stuck in between.
Now, a black screen is shown when the downloading takes place. I want to show it within a ProgressDialog so that user may feel that something is getting downloaded.
I have added a ProgressDialog, but its not showing. Can anyone tell where did I go wrong?
Below is my code:
Inside onCreate() I have written:
downloadFiles();
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
});
thread.start();
thread.run();
}
dismissProgressDialog();
return true;
}
//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() {
progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Downloading files...");
progressDialog.show();
}
private void dismissProgressDialog() {
if(progressDialog != null)
progressDialog.dismiss();
}
Try this .. it's simple
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Downloading Files...");
progress.SetCancelable(false);
RunOnUiThread(() =>
{
progress.Show();
});
Task.Run(()=>
//downloading code here...
).ContinueWith(Result=>RunOnUiThread(()=>progress.Hide()));
Please try Below Code .
private Handler responseHandler=null;
downloadFiles();
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
responseHandler.sendEmptyMessage(0);
});
thread.start();
}
responseHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
try
{
dismissProgressDialog()
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
}
Here in this code when ever your dowload will completed it called response handler and your progress dialog will dismiss.
In downloadFiles() you show the dialog, then start a number of threads and after they've been started the dialog got dismissed. I don't think this is what you want as the dialog gets closed right after the last thread is started and not after the last thread has finished.
The dismissProgressDialog() method must be called after the last thread has finished its work. So at the end of the code run in the thread you have to check whether other threads are still running or whether you can dismiss the dialog as no other threads are running.
Try the following code and let me know how it goes:
private Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
dismissProgressDialog()
}
};
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
});
thread.start();
thread.run();
}
mHandler.sendEmptyMessage(0);
return true;
}
//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() {
progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Downloading files...");
progressDialog.show();
}
private void dismissProgressDialog() {
if(progressDialog != null)
progressDialog.dismiss();
}

ProgressDialog dismissal in android

I want to open a ProgressDialog when I click on the List Item that opens the data of the clicked Item form the Web Service.
The ProgressDialog needs to be appeared till the WebContent of the clicked Item gets opened.
I know the code of using the Progress Dialog but I don't know how to dismiss it particularly.
I have heard that Handler is to be used for dismissing the Progress Dialog but I didn't found any worth example for using the Handler ultimately.
Can anybody please tell me how can I use the Handler to dismiss the Progress Dialog?
Thanks,
david
Hi this is what you want
public void onClick(View v)
{
mDialog = new ProgressDialog(Home.this);
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);
mDialog.show();
new Thread(new Runnable()
{
#Override
public void run()
{
statusInquiry();
}
}).start();
}
here is the web webservice that is called
void statusInquiry()
{
try
{
//calling webservice
// after then of whole web part you will send handler a msg
mHandler.sendEmptyMessage(10);
}
catch (Exception e)
{
mHandler.sendEmptyMessage(1);
}
}
and here goes handler code
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 10:
mDialog.dismiss();
break;
}
}
}
};
A solutiion could be this:
ProgressDialog progressDialog = null;
// ...
progressDialog = ProgressDialog.show(this, "Please wait...", true);
new Thread() {
public void run() {
try{
// Grab your data
} catch (Exception e) { }
// When grabbing data is finish: Dismiss your Dialog
progressDialog.dismiss();
}
}.start();

Categories

Resources