【问题标题】:Android ProgressDialog won't spinAndroid ProgressDialog 不会旋转
【发布时间】:2010-11-17 12:18:27
【问题描述】:

这是我的第一个 stackoverflow 帖子,所以请对我温柔一点!我确定我正在尝试做的事情是可能的,并且是我已经做过(或没有做过?)的事情导致了问题......我只是不确定那是什么something 是。

我想要做什么:
在我的应用同步和处理下载的数据时显示 ProgressDialog。

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

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

我尝试过的事情:
使用线程(下面的代码) 使用 AsynTask(如果需要可以提供代码) 使用Handler(如果需要可以提供代码)

在花费大量时间寻找我的问题的答案之后,似乎其他人也遇到了相同或相似的问题,并设法使用上述想法之一解决了它。但是,我无法实施任何这些想法来解决我的特定问题。这让我确定这是我做错了什么......我只是不确定是什么。

我编写并用作所有尝试修复的基础的原始代码:
首先

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 发生在不同的线程中,使 UI 线程除了显示 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;
}

【问题讨论】:

  • 我在进度对话框中看到了同样的冻结行为。通常是在任务几乎完成并且对话框即将关闭时。你的对话持续了多长时间?如果你让线程休眠几秒钟,那会让对话框旋转吗?
  • 我在这里询问过的 ProgressDialog 有类似的问题:stackoverflow.com/questions/3821306/…。我认为 Android 代码库中存在错误。
  • @softarn:感谢您的建议。如果我这样做: Thread doBind = new Thread(new Runnable(){ public void run(){ try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } doBindService(); } } );对话框旋转 10 秒,然后冻结 3 到 4 秒,然后消失...
  • 就像ageektrapped 说的那样,这似乎是一个错误。在我阅读问题之前,我并没有真正注意到这一点。现在真的很困扰我:) 问题:code.google.com/p/android/issues/detail?id=4266
  • 第一个问题做得好的+1

标签: java android progressdialog serviceconnection ui-thread


【解决方案1】:

微调器的问题与您在新线程中创建的 ServiceConnection 有关。不幸的是,这将在你的主线程/gui线程上运行。

来自 API 参考:(http://developer.android.com/reference/android/content/ServiceConnection.html)

就像来自系统的许多回调一样, 这个类的方法被调用 从你的进程的主线程。

所以,由于我不熟悉服务,我只能猜测,当您的服务完成下载数据时,一种通信方式是广播您在 Activity 中侦听的 Intent,然后停止 ProgressDialog。但当然还有其他方法。

附带说明,您不应该从除主线程之外的任何线程修改 gui,您试图通过生成一个尝试关闭对话框的新线程来执行此操作。您应该只从主线程中关闭对话框。

【讨论】:

  • 我正在调查您的建议(发送和响应意图),目前看起来这是我问题的答案。但是,您在帖子中说“当然还有其他方法”。 ...这些其他方式是什么?这些中的任何一个都是“更好”的方式吗?
  • 根据您的建议,我找到了这篇文章:stackoverflow.com/questions/2843874/…,现在有了一个可以运行的应用程序。感谢您的帮助!
【解决方案2】:

Hay 可以试试用Handler吗?

我认为你应该使用 Android 的 Handler 函数来做到这一点

【讨论】:

  • 嗨。谢谢你的建议。我已经尝试实现一个处理程序来执行此操作,但我不清楚对它的调用应该在哪里以及它应该实际做什么。我当前的实现仍然显示与使用线程时描述的相同的问题。我使用了helloandroid.com/tutorials/using-threads-and-progressdialog 上的示例。我将编辑我的原始帖子并添加我尝试过的代码。如果你能看看我做错了什么,我会很感激的。
【解决方案3】:
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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多