1

我已经尝试了很多方法来让这个 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每次使用它时,我都会创建一个新实例。

4

1 回答 1

0

使用 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;
}
} 
于 2013-01-07T16:18:48.127 回答