Android从Handler获取Application中的Activity实例

发布于 2024-10-08 04:06:33 字数 263 浏览 0 评论 0原文

我正在使用处理程序在当前活动和应用程序的子类之间进行通信。 子类有一个处理 Web 服务调用的线程。 子类也可能有一个 ProgressDialog 实例。

我想在 http 调用开始时显示 ProgressDialog,并在调用完成时关闭对话框。

为此,我需要从处理程序访问活动。然后使用 setOwnerActivity 将 Activity 分配给 Dialog。

这可能吗?我尝试过,但无法从处理程序获取活动。 Handler 最多可以给出发送消息的线程。

I am using Handler for communicating between Current Activity and a subclass of Application.
Subclass is having a thread which processes web service calls.
Subclass may also have a ProgressDialog instance.

I want to show ProgressDialog when http call is starting and dismiss dialog when call is finished.

For this to happen, I need to access Activity from Handler. And then assign activity to Dialog using setOwnerActivity.

Is this possible? I tried and cant not get activity from Handler. The max Handler can give is Thread which sent message.

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

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

发布评论

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

评论(2

奶茶白久 2024-10-15 04:06:33

您是否尝试过使用带有参数 msg.objMessage 发送 Activity 的实例?

顺便说一句,我认为你应该使用 AsyncTask 而不是用复杂的代码。

Have you tried sending the Instance of the Activity using a Message with parameter msg.obj?

Btw, I think you should be using a AsyncTask instead of riddling it with complicated code.

爱你不解释 2024-10-15 04:06:33

应用程序类不知道正在运行什么活动。

唯一可以做的就是通过 setter 方法让应用程序类了解活动,不建议这样做,因为它可能会导致内存问题。

The application class is unaware of what activity is running.

The only thing that can be done is to let application class know about activity is by a setter method which is not recommended as it may lead to memory problems.

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