Android getting Activity instance in Application from Handler

偶尔善良 提交于 2019-12-11 08:06:53

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4481928/android-getting-activity-instance-in-application-from-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!