在Asyn任务执行时阻止android后退按钮

发布于 2024-12-25 19:09:01 字数 1369 浏览 1 评论 0原文

当我的应用程序启动时,我正在调用 Asyn Task 函数。在后台进程中,它会命中 3 个 API 并继续解析它们的功能,大约需要 10 分钟的时间。

在这种情况下,如果用户单击 Android 默认后退按钮,则异步任务将被取消。为了避免这种情况,我尝试阻止后退按钮操作,如下所示。

class First_Time_Sync extends AsyncTask<Void, Void, Void> 
     {
        ProgressDialog dialog1 = new ProgressDialog(Progressss.this);

        protected void onPreExecute() 
        {   
            backMode = true;
            dialog1.setMessage("Welcome \nThank you for waiting.");
            dialog1.setIndeterminate(true);
            dialog1.show();
        }       

        protected Void doInBackground(Void... arg0) 
        {
            .................                   
            return null;
        }

        protected void onPostExecute(Void unused) 
        {
            backMode = false;
            dialog1.dismiss();
        }
     }

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
            if (backMode) 
            {
                return true;
            }
            else 
            {
                return super.onKeyDown(keyCode, event);
            }
        }       
        return super.onKeyDown(keyCode, event);
    }

现在,当调用异步任务函数并且用户单击后退按钮时,进度条将被取消。但后台进程仍在继续,并且后退按钮被阻止,直到后台进程完成。

我希望即使按下后退按钮也能显示进度条,这样它就不会消失。

这要怎么做......

When my app gets launched i am calling a Asyn Task function. In the background process its hitting 3 APIs and their parsing functions goes on, It will be taking around 10 minutes of time.

In that case if the user clicks on the android default back button, then the asyn task used to get cancelled. To avoid this i tried blocking the back button action as follows.

class First_Time_Sync extends AsyncTask<Void, Void, Void> 
     {
        ProgressDialog dialog1 = new ProgressDialog(Progressss.this);

        protected void onPreExecute() 
        {   
            backMode = true;
            dialog1.setMessage("Welcome \nThank you for waiting.");
            dialog1.setIndeterminate(true);
            dialog1.show();
        }       

        protected Void doInBackground(Void... arg0) 
        {
            .................                   
            return null;
        }

        protected void onPostExecute(Void unused) 
        {
            backMode = false;
            dialog1.dismiss();
        }
     }

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
            if (backMode) 
            {
                return true;
            }
            else 
            {
                return super.onKeyDown(keyCode, event);
            }
        }       
        return super.onKeyDown(keyCode, event);
    }

Now when the async task function called and if the user clicks back button progress bar gets cancelled. But the background process goes on and the back button is been blocked until the background process completes.

I want the progress bar to be shown even if the back button is pressed, so that it may not get vanished.

How to do this......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2025-01-01 19:09:01

您必须将对话框的可取消属性设置为 false

dialog1.setCancelable(false);

这将防止后退键取消对话框

You will have to set the cancelable property of the dialog to false

dialog1.setCancelable (false);

This will prevent back key from cancelling your dialog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文