我正在製作一個應用程序,其中某個任務在後臺運行時顯示進度對話框。我想在後臺任務啓動並顯示進度對話框時禁用硬件主頁按鈕,並在任務完成並且進度對話框完成時再次啓用它,以便此任務不會中斷。我試過使用startLockTask()和stopLockTask();但它總是要求許可,並且它不適用於低於21的api。 我試過下面的答案,但它沒有幫助。進度對話框運行時禁用主頁按鈕
How to disable Home and other system buttons in Android?
https://stackoverflow.com/a/23349558/2579281
據我所知,這個問題已經被問了很長一段時間了。如果有人有新的方式來做到這一點,請幫助
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]);
}
}
@ zombie我已經提到,我已經看到了答案,但這並沒有幫助,它被問了很久回 –