3

I want to show a progressBar while some instructions execute in background, but I don't want to lock the screen. This code is locking the screen:

private class ConvertDataInBackground extends AsyncTask<Params, Void, Boolean> {

        private MyProgressDialog mProgressDialog;

        @Override
        public void onPreExecute() {
            if(mProgressDialog == null)
                mProgressDialog = MyProgressDialog.show(mActivity, null, null);
        }

        @Override
        protected Boolean doInBackground(Params...params) {
            boolean conversionResult = executeConversion(params[0].item1, params[0].item2, params[0].item3);
            return conversionResult;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            try {
                if(mProgressDialog.isShowing()) 
                    mProgressDialog.dismiss();
                if(result) {
                    Toast toast = Toast.makeText(mActivity, R.string.conversion_result, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

Execution of instruction in background may take a while, and I want the user to be able to work with the interface. Do you know anyway that we can work with interface, while progressDialog is displaying at the top?

Ali
  • 9,800
  • 19
  • 72
  • 152
  • 1
    Dialogs in Android are usually modal. You can try creating the dialog then adding `FLAG_NOT_TOUCH_MODAL` to its window parameters - but I'm not sure if it will work. – Aleks G Aug 08 '13 at 11:03
  • Thanks @AleksG. You can share your answer and I'll accept. – Ali Aug 08 '13 at 12:48
  • Take a look at the `PowerManager` API and the `WakeLock` method. There's various flags you can call to prevent dimming, locking and sleeping. https://developer.android.com/reference/android/os/PowerManager.html#SCREEN_DIM_WAKE_LOCK – Joshua Pinter May 13 '17 at 22:37

2 Answers2

4

Dialogs in Android are usually modal. You can try creating the dialog then adding FLAG_NOT_TOUCH_MODAL to its window parameters - but I'm not sure if it will work. The idea behind it will be something like this:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(...);
dialog.setTitle(...);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

Note at the same time that it may be easier to just add a ProgressBar to your activity's layout and show it when needed instead of creating a dialog - this is, in fact, what Google documentation recommends. Android developer's guide on dialogs has this to say:

Avoid ProgressDialog

Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. However, if you need to indicate loading or indeterminate progress, you should instead follow the design guidelines for Progress & Activity and use a ProgressBar in your layout.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
2

I think that in your activity you must specify the FLAG_KEEP_SCREEN_ON flag:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

For me it worked.

Ivan
  • 4,186
  • 5
  • 39
  • 72