How to call an Activity's non-static method from inside a service onStart()?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
How to call an Activity's non-static method from inside a service onStart()?
You can't.
However, there are many ways to have a service communicate with a running activity, and I outline some of them here: How can I update information in an Android Activity from a background Service
回答2:
This could help ..
In activity define
static MyActivity instance;
then set value MyActivity.OnCreate
instance = this;
In your service
MyActivity activity = MyActivity.instance; if (activity != null) { // we are calling here activity's method }