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]);
}
}