Android中UI线程操作期间显示ProgressDialog

发布于 2024-12-15 20:38:19 字数 590 浏览 1 评论 0原文

正如我在另一个问题此处提出的那样,似乎 PackageManager.getInstalledPackages() 没有不能很好地使用线程。正如 CommonsWare 所说此处:

也许需要在主应用程序上调用 PackageManager 线程,由于某种原因

因此将其放在线程上会出现不良行为,在我的活动中进入和退出几次会使显示的应用程序列表有时带有项目,有时为空。让 UI 线程中的所有内容都像梦想一样工作,每次都能正常加载。问题是,用户期望得到某种反馈,而我需要提供反馈。当我开始活动时,屏幕保持黑屏 3-4-5-6 秒(取决于安装的设备和应用程序)。我如何提供某种反馈?我正在考虑一个 ProgressDialog,但我不知道如何启动它。谢谢。

As I've asken on another question HERE it seems that the PackageManager.getInstalledPackages() doesn't play nice with Threading. As CommonsWare stated HERE:

Perhaps the PackageManager needs to be invoked on the main application
thread, for some reason

So having it on a thread gets undesired behavior, entering and exiting a few times in my Activity makes the list of displayed apps sometimes with items, sometimes empty. Having everything in the UI Thread works like a dream, loads fine everytime. The thing is, the user expects some sort of feedback and I need to provide one. As I start the activity, the screen remains black for 3-4-5-6 seconds (depending on the device and apps installed). How can I provide some sort of feedback ? I am thinking of a ProgressDialog but I don't know how can I start it. Thank you.

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

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

发布评论

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

评论(3

蓝海 2024-12-22 20:38:19

正如我们所发现的,与调用 PackageManager.getInstalledPackages()(必须在 UI 线程上完成)相比,处理应用程序的循环需要一段时间(可以在单独的线程中完成)。

As discovered, the loop to work through the applications takes awhile (which can be done in a separate thread), compared to the call to PackageManager.getInstalledPackages() (which has to be done on the UI thread).

梦在深巷 2024-12-22 20:38:19

使用异步执行后台工作并在加载数据时显示指示器。

在你的 onCreate() 中。调用 new AsyncCommonDataFetcher(this).execute();

public class AsyncCommonDataFetcher extends AsyncTask<Void, Void, Void> {

    Context mContext = null;
    private ProgressDialog mProgressIndicator = null;

    public AsyncCommonDataFetcher(Context ctx) {
        mContext = ctx;
    }

protected void onPreExecute() {
        mProgressIndicator = ProgressDialog.show(((Activity) mContext), null,
                    "Please wait", true);

    }
    @Override
    protected Void doInBackground(Void... voids) {
        // Do what ever work you like to do. It will do this in backgound and show user a indicator to wait.
    }

    @Override
    protected void onPostExecute(Void voidInstance) {


            try {
                if (mProgressIndicator != null) {
                    mProgressIndicator.hide();
                    mProgressIndicator.dismiss();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }
}

Use Async to do background work and show indicator while loading data.

in you onCreate(). call new AsyncCommonDataFetcher(this).execute();

public class AsyncCommonDataFetcher extends AsyncTask<Void, Void, Void> {

    Context mContext = null;
    private ProgressDialog mProgressIndicator = null;

    public AsyncCommonDataFetcher(Context ctx) {
        mContext = ctx;
    }

protected void onPreExecute() {
        mProgressIndicator = ProgressDialog.show(((Activity) mContext), null,
                    "Please wait", true);

    }
    @Override
    protected Void doInBackground(Void... voids) {
        // Do what ever work you like to do. It will do this in backgound and show user a indicator to wait.
    }

    @Override
    protected void onPostExecute(Void voidInstance) {


            try {
                if (mProgressIndicator != null) {
                    mProgressIndicator.hide();
                    mProgressIndicator.dismiss();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }
}
娇妻 2024-12-22 20:38:19

在活动的 onCreate() 中尝试对 ProgressDialog 执行以下操作

this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
ProgressDialog LoadingDialog = ProgressDialog.show(this, "", "Loading..", true);

,然后在导致延迟的过程结束时将其关闭

LoadingDialog.dismiss();

Try the following for ProgressDialog in the onCreate() of your activity

this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
ProgressDialog LoadingDialog = ProgressDialog.show(this, "", "Loading..", true);

And then dismiss it when the process causing the delay is over

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