Android access activity method inside service

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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          } 


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