Android从Handler获取Application中的Activity实例
我正在使用处理程序在当前活动和应用程序的子类之间进行通信。 子类有一个处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用带有参数
msg.obj
的Message
发送 Activity 的实例?顺便说一句,我认为你应该使用 AsyncTask 而不是用复杂的代码。
Have you tried sending the Instance of the Activity using a
Message
with parametermsg.obj
?Btw, I think you should be using a AsyncTask instead of riddling it with complicated code.
应用程序类不知道正在运行什么活动。
唯一可以做的就是通过 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.