Android: ProgressDialog not spinning

我与影子孤独终老i 提交于 2019-12-13 06:49:17

问题


I am having problems getting the ProgressDialog wheel spinning. Here is my code:

final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true, false);
Thread thread=new Thread(new Runnable(){
    public void run(){
    runOnUiThread(new Runnable(){
        @Override
        public void run() {
            if(dialog.isShowing())
                // starts a foreground service, does database stuff, 
                // sets up a spinner with values
                dialog.dismiss();
            }

        });
    }
});

thread.start();

Everything goes as planned, I get the ProgressDialog, stuff happens in the background and once set, ProgressDialog goes away - the only problem is that the animation in ProgressDialog is not spinning, pretty much rendering it useless.

What am I doing wrong?


回答1:


The code you omitted here

        // starts a foreground service, does database stuff, 
        // sets up a spinner with values

must do something that block the UI thread. Just put them outside the runOnUiThread() Method.

Thread thread=new Thread(new Runnable(){
    public void run(){
        // starts a foreground service, does database stuff, 
        // sets up a spinner with values
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
            if(dialog.isShowing())
                dialog.dismiss();
            }

        });
    }
});



回答2:


ProgressDialog example using handler android

    final ProgressDialog dialog = ProgressDialog.show(this, "Title",
            "Message", true);

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();
        }
    };
    Thread t = new Thread() {
        public void run() {
            // write process code here.

            handler.sendEmptyMessage(0);
        }
    };
    t.start();

Fix issue: ProgressDialog not working




回答3:


Because you put the processing dialog in to wrong area, for example with my error: I have a two activities: MainActivity and ShowingActivity, MainActivity will show processing dialog after new intent tranfer to ShowingActivity and ShowingActivity will get some DATA from server (it will be blocked here). When I call ProcessingDialog in MainActivity, it showed but not spinning because my Intent move to ShowingActivity and it block Ui because action of get DATA from server must wait some seconds. So I fixed it: 1/ in MainActivity call:

final Intent working = new Intent(getApplicationContext(),
                    WorkingActivity.class);
final ProgressDialog ringProgressDialog = ProgressDialog.show(
                    MainActivity.this, "Please wait ...", "Connecting ...",
                    true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //Menthod need time to load:
                                    getDatafromServer();
                             if (BookListFragment.isLoaded) {
                                ringProgressDialog.dismiss();
                                startActivity(working);
                                return;
                             }
});

So it will not block Ui because MainActivity still runing after DATA was gotten. And in ShowingActivity I will use this DATA (because I set DATA is a static String).



来源:https://stackoverflow.com/questions/10267930/android-progressdialog-not-spinning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!