[英]GetActivity from IntentService in Android
I have an IntentService class 我有一个IntentService类
public class eventoTimer extends IntentService {}
public Utilities(Activity activity) {
this.activity = activity;
this.session = new UserSessionManager(this.activity);
}
And I need to instantiate the Utilities class with an Activity but I do not know how to call the getActivity from the IntentService 而且我需要使用Activity实例化Utilities类,但是我不知道如何从IntentService调用getActivity。
And I need to instantiate the Utilities class with an Activity but I do not know how to call the getActivity from the IntentService
而且我需要使用Activity实例化Utilities类,但是我不知道如何从IntentService调用getActivity。
You can't. 你不能 For example, there is no requirement that there be an activity, as the user could have destroyed your activity while your
IntentService
is doing its work. 例如,不要求有是一个活动,因为用户可能已经摧毁了你的活动,而你的
IntentService
正在做的工作。
Either: 要么:
Change Utilities
to take a Context
, rather than an Activity
(so you can pass in the IntentService
to the Utilities
constructor), or 将
Utilities
更改为Context
而不是Activity
(以便您可以将IntentService
传递给Utilities
构造函数),或者
Use an event bus (eg, LocalBroadcastManager
, greenrobot's EventBus) to send a message to your activity, if it exists, to have it do something, or 使用事件总线(例如
LocalBroadcastManager
,greenrobot的EventBus)将消息发送到您的活动(如果存在),以使其执行某项操作,或者
Get rid of the IntentService
, if it is so tightly coupled to the activity, and do something else for your background threading (eg, AsyncTask
) 如果
IntentService
与活动紧密IntentService
,请摆脱它,并为后台线程做其他事情(例如AsyncTask
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.