-1

I am making an app in which I am showing a progress dialog while some task is running in background . I want to disable hardware home button when background task starts and progress dialog is shown and enable it again when task is completed and progress dialog is finished so that this task can't be interrupted. I have tried using startLockTask() and stopLockTask(); but it always ask for permission and it doesn't work with api lower than 21. I have tried following answer but it didn't help.

How to disable Home and other system buttons in Android?

https://stackoverflow.com/a/23349558/2579281

I understand that this question has already been asked a long time back. If someone has new way to do this please help

class MyTaskRunner extends AsyncTask<String, String, String> {

        private String resp;
        ProgressDialog progressDialog;

        @Override
        protected String doInBackground(String... params) {
            publishProgress("Sleeping..."); // Calls onProgressUpdate()
            try {
                int time = Integer.parseInt(params[0])*1000;

                Thread.sleep(time);
                resp = "Slept for " + params[0] + " seconds";
            } catch (InterruptedException e) {
                e.printStackTrace();
                resp = e.getMessage();
            } catch (Exception e) {
                e.printStackTrace();
                resp = e.getMessage();
            }
            return resp;
        }


        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            progressDialog.dismiss();
            //enable home button here
            //stopLockTask();

        }


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(MainActivity.this,
                    "ProgressDialog","Wait..");
                    //disable home button here
                    //startLockTask();
        }


        @Override
        protected void onProgressUpdate(String... text) {
            finalResult.setText(text[0]);

        }
}
Community
  • 1
  • 1
Vikash Kumar Verma
  • 1,068
  • 2
  • 14
  • 30

3 Answers3

3

Apps are not allowed to disable HOME button. Then you should do the download (or any other lengthy operation or process) in a background service and let the UI connect to it when running.

If the user presses HOME you Activity is paused but download continues, when the user navigates back to your Activity you update the progress.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You can disable the hardware back button by overriding onBackPressed() in your activity with some sort of logic to determine whether or not the back action should be carried out. I'm not sure if it's possible to override the hardware home button.

@Override
public void onBackPressed() {
    if (allowBack) {
        super.onBackPressed();
    }
}
Abtin Gramian
  • 1,630
  • 14
  • 13
  • I want to disable home button I already have disabled back button. – Vikash Kumar Verma Oct 13 '16 at 20:18
  • Maybe you can try overriding the behavior in one of the activity lifecycle events such as `onPause()` to not leave the current activity while the download is taking place. Why exactly do you need to prevent the user from leaving the current view? Why not use [DownloadManager](https://developer.android.com/reference/android/app/DownloadManager.html)? – Abtin Gramian Oct 13 '16 at 20:25
  • actually I am not downloading anything I have use that work make my problem clear. – Vikash Kumar Verma Oct 13 '16 at 20:29
0

Try this, dialog is an object of ProgressDialog.

    dialog.setCancelable(false);
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes();
    wlp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    window.setAttributes(wlp);