代码之家  ›  专栏  ›  技术社区  ›  John

Android ProgressDialog无法旋转

  •  5
  • John  · 技术社区  · 14 年前

    这是我的第一个堆垛溢出柱,所以请温柔地对待我!我确信我所做的是可能的,这是我已经做过(或没有做)的事情。这就是问题的根源…我只是不确定那是什么 某物 是。

    我想做的是:
    在应用程序同步和处理下载的数据时显示ProgressDialog。

    问题:
    progressdialog显示但不旋转(这使得它看起来像被冻结了),同步和处理发生,progressdialog关闭,应用程序继续正常运行。

    我目前正在努力做到这一点:
    在我的活动中创建ProgressDialog
    进行同步-在我的服务中
    处理数据-在我的服务中
    关闭ProgressDialog-在我的活动中

    我尝试过的事情:
    使用线程(下面的代码) 使用asyntask(如果需要可以提供代码) 使用处理程序(如果需要,可以提供代码)

    在花了很多时间寻找我的问题的答案之后,其他人似乎也遇到了同样或相似的问题,并用上面的一个想法解决了它。然而,我一直无法实现这些想法来解决我的特定问题。这让我确信这是我做错了什么…我只是不确定。

    我编写并使用的原始代码作为我所有尝试修复的基础:
    弗斯特

    public class myActivity extends ListActivity {
        private ProgressDialog dialog;
        private boolean mIsBound;
        private myService mBoundService;
        private ServiceConnection mConnection;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            /*if we need to login start login activity for result*/
            ...
        }
    
        ...
        /*other methods*/
        ...
    }
    

    然后

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode){
            case ACTIVITY_LOGIN:
                /*after login we know we always need to sync*/
             dialog = new ProgressDialog(myActivity.this);
             dialog.setMessage("Synchronising...");
             dialog.setIndeterminate(true);
             dialog.setCancelable(false);
             dialog.show();
    
                Thread doBind = new Thread(new Runnable(){public void run(){doBindService();}});
                doBind.start();
                break;
        }
    }
    

    所以现在我假设dobind是在另一个线程中发生的,除了显示ProgressDialogue之外,用户界面线程什么都不做。

    private boolean doBindService() {
     mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                mBoundService = ((myService.LocalBinder)service).getService();      
                while (mBoundService.isRunning()){
                    /*wait for service to finish before doing anything else... myMethod() is expecting the sync to have completed*/
                    /*I suspect this loop to be the thing causing the problem. But I'm not sure why because it is in a different thread so shouldn't interfear with the dialog?! And if that is what is causing the problem then how else can I do this?*/
                }
                /*get the activity to do Stuff with the downloaded data*/
                myMethod();
                /*finished with the service for now so unbind*/
                doUnbindService();
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
    
            public void onServiceDisconnected(ComponentName className) {
                mBoundService = null;
            }
        };
    
        boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = myReturn;
     return myReturn;
    }
    
    private void doUnbindService() {
        if (mIsBound) {
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    

    如果您需要任何进一步的信息来帮助我解决这个问题,请告诉我。


    编辑:

    下面是我用来使用处理程序的代码。这将显示与以前相同的行为(微调器不旋转)。

    Thread doBind = new Thread(new Runnable(){
        public void run(){
            doBindService();
        }
    });
    doBind.start();
    

    .

    private Handler handler = new Handler() {
        @Override
    public void handleMessage(Message msg) {
        dialog.dismiss();
    }
    };
    

    .

    private boolean doBindService() {
     mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                mBoundService = ((myService.LocalBinder)service).getService();      
                while (mBoundService.isRunning()){
                }
                //...do stuff same as before...
                handler.sendEmptyMessage(0);
            }
    
            public void onServiceDisconnected(ComponentName className) {
                mBoundService = null;
            }
        };
    
        boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = myReturn;
     return myReturn;
    }
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   Cpt.Ohlund    14 年前

        2
  •  0
  •   Piyush    14 年前

        3
  •  0
  •   Pang Ajmal PraveeN    9 年前
    public class yourclass extends Activity implements Runnable {
    
        static ProgressDialog progressDialog;
    
        @Override
        public void onCreate(Bundle icicle) {
                    progressDialog = ProgressDialog.show(keywordview.this, "", "Loading...", true);
                    Thread thread = new Thread(keywordview.this);
    
        }
        final Handler handler = new Handler() {
    
            @Override
            public void handleMessage(Message msg) {
                progressDialog.dismiss();
            /*Here you can handle you process*/
            }
        };
    
        public void run() {
            //here your code for run 
            handler.sendEmptyMessage(0);
        }
    }