What i want to do is starting a new thread when i click a button and
this new thread will do some long-time tasks. A loading dialog will be
shown before that thread starts and will be hidden when the thread
ends.If i wait until the thread finishes its job and ends normally,
everything will be OK. But if i cancel the loading dialog and
"interrupt" the thread in the cancel event handler of that dialog, an
UnCaughtExcetpion will be thrown.

Quote:
Can't create handler inside the thread that has not called
Looper.prepare()


Full code: AsyncLoadingMgrSample .java

Java:

package ciente.cl;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.FutureTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author rader
 *
 */
public class AsyncLoadingMgrSample extends Activity {

     private final AsyncLoadingMgr mgr=new AsyncLoadingMgr(this);
     protected Runnable cancelLoding;
     protected FutureTask<?> loading;;
     ProgressDialog mProgDlg;

     final public FinishlLoadingTask finishLoadingTask=new
FinishlLoadingTask();

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.asyncloadingmgrsample);

//        loading=new FutureTask<String>(new LoadingTask(this));
//        cancelLoding=new CancelLoadingTask();

          final Handler handler=new Handler();


          Button btnAsyncLoad=(Button)findViewById(R.id.btnAsyncLoad);
          btnAsyncLoad.setOnClickListener(new OnClickListener(){

               @Override
               public void onClick(View arg0){
                    EditText
txt=(EditText)findViewById(R.id.txtAsyncLoad);

 //                    Executor
executor=java.util.concurrent.Executors.newFixedThreadPool(1);
 //                    CompletionService<String> completionService =
 //                       new
ExecutorCompletionService<String>(executor);
                    txt.setText("start task...");
                    try {
//                       Callable task=new LoadingTask(handler);
                         final Thread loadingThread=new Thread(new
LoadingTask2(handler));
                         loadingThread.start();

                         mProgDlg =
ProgressDialog.show(AsyncLoadingMgrSample.this, null,
                                   "Please wait while loading...",
true, true);
                         mProgDlg.setCancelListener(new
OnCancelListener(){

                              @Override
                              public void onCancel(DialogInterface
arg0) {
                                   loadingThread.interrupt();

//                                 while(!
loadingThread.isInterrupted()){
//
//                                 }
 
Toast.makeText(AsyncLoadingMgrSample.this,"loading task
interruptted",Toast.LENGTH_LONG).show();
 
Log.i("CancelLoadingTask:Run","loading task interruptted");
                                   EditText
txt=(EditText)findViewById(R.id.txtAsyncLoad);
                                   txt.setText("loading task
interruptted");

                              }

                         });
//                       completionService.submit(task);
                     txt.setText("wait for the task ending....");
                    } catch (Exception e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         Log.e("CancelLoadingTask:Run","loading task
canceled["+e.toString());
                    }

               }

          });
     }

     class CancelLoadingTask implements Runnable{

          @Override
          public void run() {
               if(mProgDlg!=null)
                    mProgDlg.dismiss();
               EditText txt=(EditText)findViewById(R.id.txtAsyncLoad);
               txt.setText("loading task canceled");
               Toast.makeText(AsyncLoadingMgrSample.this,"loading task
canceled",Toast.LENGTH_LONG).show();
               Log.i("CancelLoadingTask:Run","loading task canceled");

          }

     }
     class FinishlLoadingTask implements Runnable{

          @Override
          public void run() {
               if(mProgDlg!=null)
                    mProgDlg.dismiss();
               EditText txt=(EditText)findViewById(R.id.txtAsyncLoad);
               txt.setText("loading task finshed");
               Toast.makeText(AsyncLoadingMgrSample.this,"loading task
finshed",Toast.LENGTH_LONG).show();
               Log.i("FinishlLoadingTask:Run","loading task finshed");

          }

     }
     class LoadingTask2 implements Runnable{
          private Handler localHandler;
          private Handler remoteHandler;
          public LoadingTask2(Handler remotehandler){
               this.remoteHandler=remotehandler;
               localHandler=new Handler();
          }
          public Handler getLocalHandler(){
               return localHandler;
          }
          @Override
          public void run() {
               try {
                    Thread.sleep(10000);
//                  if(Thread.interrupted())
//                       remoteHandler.post(new CancelLoadingTask());
//                  else
//                       remoteHandler.post(new FinishlLoadingTask());
 
remoteHandler.post(AsyncLoadingMgrSample.this.finishLoadingTask);
               } catch (InterruptedException e) {
                    Toast.makeText(AsyncLoadingMgrSample.this,"loading
task error",Toast.LENGTH_LONG).show();
                    Log.i("LoadingTask2:Run","loading task error");
               }

          }

     }

[quote]

"remoteHandler" in class LoadingTask2 means a Handler in UI thread.

Thanks for help in advance!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to