进度对话框在长时间计算中无法正常工作



我已经尝试了很多事情来让这个ProgressDialog正常工作,但我仍然没有得到它100%。我基本上已经走到了两个不同的死胡同,离最终目标同样近。

首先,我尝试创建一个额外的Thread并使用runOnUiThread。这导致对话框showingdismissing在适当的时间,我想要它,但旋转器不旋转。它静止不动,从不调整。

readButton = (Button) findViewById( R.id.readButton );
    readButton.setOnClickListener( new OnClickListener() {
        public void onClick(View arg0) {

            final ProgressDialog pd = new ProgressDialog( Waves.this );  
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pd.setMessage("Reading...");
            pd.setCancelable(false);            
            pd.show( );
            Thread export = new Thread() {  
                 public void run() {
                        if( myService.getState() != 3 ) {
                            runOnUiThread( new Runnable() {
                                public void run() {
                                    Log.i( "myService", "12222" ); 
                                    myService.setHandler( mHandler );
                                    myService.connect( MainMenu.previousDevice, true );
                                }
                            });
                        } else {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    readWaves();
                                }
                            });
                        }
                 mHandler.obtainMessage(MainMenu.READ_FINISHED ).sendToTarget();
                 pd.dismiss();
                 }
            };
            export.start();
            }
    });

我想做的下一件事是使用AsyncTask。从我在互联网上发现的一切,所以这看起来像是要走的路。旋转器开始旋转,我的代码开始工作,但在很短的时间后,我的应用程序离开当前活动并完全重新启动。LogCat如下所示,但它没有显示我任何错误。

 readButton = (Button) findViewById( R.id.readButton );
    readButton.setOnClickListener( new OnClickListener() {
        public void onClick(View arg0) {
            new readPage().execute( null, null, null );
            }
    });

 public class readPage extends AsyncTask<Void, Void, Void> {
        private ProgressDialog pdia;
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
                pdia = new ProgressDialog( Waves.this);
                pdia.setMessage("Reading...");
                pdia.setCancelable( false );
                pdia.show();
            }
        @Override
        protected void onPostExecute(Void unused) {
            pdia.dismiss();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            Log.i( "ASYNC", "2" );
            if( myService.getState() != 3 ) {
                myService.setHandler( mHandler );
                myService.connect( MainMenu.previousDevice, true );
            } else {
                readWaves();
            }
            return null;
        }
}

这是LogCat。显示的第一行是LogCat中的最后一件事,这是我代码的一部分。我还没有检查这一行发生了什么,因为它总是有效的,只是没有实现这个AsyncTask。最后一行有一个Main Menu ++ ON CREATE ++条目,这是我的应用程序重新启动的时候。

01-07 18:23:27.738: W/NATIVE CODE(28111): After SetBaud
01-07 18:23:29.777: I/dalvikvm(28111): threadid=4: reacting to signal 3
01-07 18:23:30.781: W/dalvikvm(28111): threadid=4: spin on suspend #1 threadid=9 (pcf=0)
01-07 18:23:30.781: D/dalvikvm(28111): Temporarily moving tid 28128 to fg (was 0)
01-07 18:23:30.781: D/dalvikvm(28111): Temporarily raised priority on tid 28128 (10 -> 0)
01-07 18:23:31.527: W/dalvikvm(28111): threadid=4: spin on suspend #2 threadid=9 (pcf=3)
01-07 18:23:31.531: I/dalvikvm(28111): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
01-07 18:23:31.531: I/dalvikvm(28111):   | group="system" sCount=0 dsCount=0 obj=0x4050f730 self=0x117d60
01-07 18:23:31.531: I/dalvikvm(28111):   | sysTid=28114 nice=0 sched=0/0 cgrp=default handle=582800
01-07 18:23:31.531: I/dalvikvm(28111):   at dalvik.system.NativeStart.run(Native Method)
01-07 18:23:31.531: I/dalvikvm(28111): "AsyncTask #1" prio=5 tid=9 RUNNABLE
01-07 18:23:31.531: I/dalvikvm(28111):   | group="main" sCount=1 dsCount=0 obj=0x40559388 self=0x2e1a10
01-07 18:23:31.531: I/dalvikvm(28111):   | sysTid=28128 nice=0 sched=0/0 cgrp=default handle=3243272
01-07 18:23:31.925: I/dalvikvm(28111):   at my.eti.commander.RelayAPIModel$NativeCalls.GetWavesJava(Native Method)
01-07 18:23:31.933: I/dalvikvm(28111):   at my.eti.commander.Waves.readWaves(Waves.java:365)
01-07 18:23:31.937: I/dalvikvm(28111):   at my.eti.commander.Waves.access$0(Waves.java:350)
01-07 18:23:31.941: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:323)
01-07 18:23:31.945: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:1)
01-07 18:23:31.949: I/dalvikvm(28111):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-07 18:23:31.957: I/dalvikvm(28111):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-07 18:23:31.960: I/dalvikvm(28111):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-07 18:23:31.964: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-07 18:23:31.968: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-07 18:23:31.972: I/dalvikvm(28111):   at java.lang.Thread.run(Thread.java:1019)
01-07 18:23:32.734: W/dalvikvm(28111): threadid=4: spin on suspend #3 threadid=9 (pcf=3)
01-07 18:23:32.734: I/dalvikvm(28111): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
01-07 18:23:32.734: I/dalvikvm(28111):   | group="system" sCount=0 dsCount=0 obj=0x4050f730 self=0x117d60
01-07 18:23:32.734: I/dalvikvm(28111):   | sysTid=28114 nice=0 sched=0/0 cgrp=default handle=582800
01-07 18:23:32.734: I/dalvikvm(28111):   at dalvik.system.NativeStart.run(Native Method)
01-07 18:23:32.734: I/dalvikvm(28111): "AsyncTask #1" prio=5 tid=9 RUNNABLE
01-07 18:23:32.734: I/dalvikvm(28111):   | group="main" sCount=1 dsCount=0 obj=0x40559388 self=0x2e1a10
01-07 18:23:32.734: I/dalvikvm(28111):   | sysTid=28128 nice=0 sched=0/0 cgrp=default handle=3243272
01-07 18:23:32.859: I/dalvikvm(28111):   at my.eti.commander.RelayAPIModel$NativeCalls.GetWavesJava(Native Method)
01-07 18:23:32.859: I/dalvikvm(28111):   at my.eti.commander.Waves.readWaves(Waves.java:365)
01-07 18:23:32.859: I/dalvikvm(28111):   at my.eti.commander.Waves.access$0(Waves.java:350)
01-07 18:23:32.859: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:323)
01-07 18:23:32.859: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:1)
01-07 18:23:32.859: I/dalvikvm(28111):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-07 18:23:32.859: I/dalvikvm(28111):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-07 18:23:32.859: I/dalvikvm(28111):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-07 18:23:32.859: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-07 18:23:32.859: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-07 18:23:32.863: I/dalvikvm(28111):   at java.lang.Thread.run(Thread.java:1019)
01-07 18:23:33.613: W/dalvikvm(28111): threadid=4: spin on suspend #4 threadid=9 (pcf=3)
01-07 18:23:33.613: I/dalvikvm(28111): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
01-07 18:23:33.613: I/dalvikvm(28111):   | group="system" sCount=0 dsCount=0 obj=0x4050f730 self=0x117d60
01-07 18:23:33.613: I/dalvikvm(28111):   | sysTid=28114 nice=0 sched=0/0 cgrp=default handle=582800
01-07 18:23:33.613: I/dalvikvm(28111):   at dalvik.system.NativeStart.run(Native Method)
01-07 18:23:33.613: I/dalvikvm(28111): "AsyncTask #1" prio=5 tid=9 RUNNABLE
01-07 18:23:33.617: I/dalvikvm(28111):   | group="main" sCount=1 dsCount=0 obj=0x40559388 self=0x2e1a10
01-07 18:23:33.617: I/dalvikvm(28111):   | sysTid=28128 nice=0 sched=0/0 cgrp=default handle=3243272
01-07 18:23:33.617: I/dalvikvm(28111):   at my.eti.commander.RelayAPIModel$NativeCalls.GetWavesJava(Native Method)
01-07 18:23:33.617: I/dalvikvm(28111):   at my.eti.commander.Waves.readWaves(Waves.java:365)
01-07 18:23:33.617: I/dalvikvm(28111):   at my.eti.commander.Waves.access$0(Waves.java:350)
01-07 18:23:33.617: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:323)
01-07 18:23:33.617: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:1)
01-07 18:23:33.617: I/dalvikvm(28111):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-07 18:23:33.617: I/dalvikvm(28111):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-07 18:23:33.617: I/dalvikvm(28111):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-07 18:23:33.617: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-07 18:23:33.617: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-07 18:23:33.617: I/dalvikvm(28111):   at java.lang.Thread.run(Thread.java:1019)
01-07 18:23:34.367: W/dalvikvm(28111): threadid=4: spin on suspend #5 threadid=9 (pcf=3)
01-07 18:23:34.367: I/dalvikvm(28111): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
01-07 18:23:34.367: I/dalvikvm(28111):   | group="system" sCount=0 dsCount=0 obj=0x4050f730 self=0x117d60
01-07 18:23:34.367: I/dalvikvm(28111):   | sysTid=28114 nice=0 sched=0/0 cgrp=default handle=582800
01-07 18:23:34.371: I/dalvikvm(28111):   at dalvik.system.NativeStart.run(Native Method)
01-07 18:23:34.371: I/dalvikvm(28111): "AsyncTask #1" prio=5 tid=9 RUNNABLE
01-07 18:23:34.371: I/dalvikvm(28111):   | group="main" sCount=1 dsCount=0 obj=0x40559388 self=0x2e1a10
01-07 18:23:34.371: I/dalvikvm(28111):   | sysTid=28128 nice=0 sched=0/0 cgrp=default handle=3243272
01-07 18:23:34.371: I/dalvikvm(28111):   at my.eti.commander.RelayAPIModel$NativeCalls.GetWavesJava(Native Method)
01-07 18:23:34.371: I/dalvikvm(28111):   at my.eti.commander.Waves.readWaves(Waves.java:365)
01-07 18:23:34.371: I/dalvikvm(28111):   at my.eti.commander.Waves.access$0(Waves.java:350)
01-07 18:23:34.371: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:323)
01-07 18:23:34.371: I/dalvikvm(28111):   at my.eti.commander.Waves$readPage.doInBackground(Waves.java:1)
01-07 18:23:34.371: I/dalvikvm(28111):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-07 18:23:34.371: I/dalvikvm(28111):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-07 18:23:34.371: I/dalvikvm(28111):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-07 18:23:34.371: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-07 18:23:34.371: I/dalvikvm(28111):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-07 18:23:34.371: I/dalvikvm(28111):   at java.lang.Thread.run(Thread.java:1019)
01-07 18:23:35.253: E/MainMenu(28151): +++ ON CREATE +++
编辑:

我还想添加我已经尝试使用.hide()而不是.dismiss(),但我不认为这是问题所在。每次使用ProgressDialog时,我都会创建一个新的实例。

使用AsyncTask在后台运行你的长计算过程。你可以在OnPreExecute()上显示你的进度对话框。当计算完成时,这个异步方法将调用onPostExecute(),在那里您可以取消进度对话框。你的计算编码在doInBackground()里面。

public class LongCaluclation extends AsyncTask<Void, Void, Void>
{  
@Override
protected void onPreExecute()
{  
    super.onPreExecute();
    dialog.setMessage("Calculation in progress please wait...");
    dialog.setCancelable(false);
    dialog.show();
} 
@Override 
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
 }
@Override 
protected void onPostExecute(Void result)
{ 
      //Post Execute
       dialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
  // Your operation..Dont Edit Any Views here
    return null;
}
} 

相关内容

  • 没有找到相关文章

最新更新