Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓ProgressDialog赢得';旋转_Java_Android_Progressdialog_Serviceconnection_Ui Thread - Fatal编程技术网

Java 安卓ProgressDialog赢得';旋转

Java 安卓ProgressDialog赢得';旋转,java,android,progressdialog,serviceconnection,ui-thread,Java,Android,Progressdialog,Serviceconnection,Ui Thread,这是我的第一个帖子,所以请对我温柔一点!我确信我正在尝试做的是可能的,是我做过(或没有做?)的事情导致了问题。。。我只是不确定那是什么 我想做什么: 当我的应用程序同步和处理下载的数据时,显示ProgressDialog 问题: ProgressDialog显示但不旋转(这使它看起来像已冻结),同步和处理发生,ProgressDialog关闭,应用程序继续正常运行 我目前如何努力做到这一点: 创建进度对话框-在我的活动中 执行同步-在我的服务中 处理数据-在我的服务中 这是我的活动中的Progr

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

我想做什么:
当我的应用程序同步和处理下载的数据时,显示ProgressDialog

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

我目前如何努力做到这一点:
创建进度对话框-在我的活动中
执行同步-在我的服务中
处理数据-在我的服务中
这是我的活动中的ProgressDialog

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

在花了很多时间寻找我的问题的答案后,似乎其他人也有相同或类似的问题,并使用上面的一个想法设法解决了它。然而,我一直无法实施任何这些想法来解决我的特定问题。这让我确信这是我做错的事情。。。我只是不知道是什么

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

然后

所以现在我假设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();


你能试着使用Handler吗


我认为您应该使用Android的Handler函数来实现这一点。微调器的问题与您在新线程中创建的ServiceConnection有关。不幸的是,这将在主线程/gui线程上运行

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

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

因此,由于我不熟悉服务,我只能猜测,当您的服务完成下载数据时,一种通信方式是广播您在活动中收听的意图,然后停止ProgressDialog。但肯定还有其他方法


另外,除了主线程之外,您不应该从任何线程修改gui,您正试图通过生成一个新线程来关闭对话框来完成此操作。您应该只从主线程中关闭对话框。

我在进度对话框中看到了相同的冻结行为。通常是在任务即将完成且对话框即将关闭时。你的对话持续了多长时间?如果让线程休眠几秒钟,是否会导致对话框旋转?我在这里询问ProgressDialog时遇到了类似的问题:。我认为Android代码库中有一个bug。@softarn:谢谢你的建议。如果我这样做:Thread doBind=new Thread(new Runnable(){public void run(){try{Thread.sleep(10000);}catch(InterruptedException e){e.printStackTrace();}doBindService();}});对话框旋转10秒,然后冻结3到4秒,然后消失…就像Ageeked说的,这似乎是一个bug。在我读这个问题之前,我并没有真正注意到这一点。现在它真的困扰着我:)问题:+1对于做得好的第一个问题嗨。谢谢你的建议。我已经尝试实现一个处理程序来实现这一点,但我不清楚对它的调用应该在哪里,以及它应该实际做什么。我当前的实现仍然显示与使用线程时描述的问题相同的问题。我使用了上所示的示例。我将编辑我的原始帖子并添加我尝试过的代码。如果你能看看我做错了什么,我会很感激的。我正在调查你的建议(发送并回应一个意图),目前看来这就是我问题的答案。然而,你在你的帖子中说“当然还有其他的方法。”。。。还有其他的方法吗?这些都是一个“更好”的方法吗?根据你的建议,我发现了这个帖子:现在有了一个可以工作的应用程序。谢谢你的帮助!
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);
    }
}
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;
}
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);
    }
}