问题描述
我想在我的活动
获得 ServiceState
的一个实例。但我怎么做到这一点?没有静态方法来获得实例或返回一个 ServiceState
实例的任何服务的任何方法。结果
还有就是 TelephonyManager.listen()
电话。但我想获得 ServiceState
实例时,我想,如果不是因为什么改变了Android把我的听众。
I'm trying to obtain an instance of ServiceState
in my Activity
. But how am I supposed to do this? There is no static method to obtain an instance or any method on any service that returns an ServiceState
instance.
There is the TelephonyManager.listen()
call. But I want to get the ServiceState
instance when I want, not when Android calls my listener because something changed.
ServiceState
的文档可以在这里找到:的
The documentation of ServiceState
can be found here: http://developer.android.com/reference/android/telephony/ServiceState.html
推荐答案
我想我真的我以前不明白你的问题,因为据我所知,自从API等级1,还有的一直在ServiceState一个空的构造是上市。所以,如果你想它的一个实例,你需要做的是:
I think I did't really understand your question since as far as I can tell, ever since API level 1, there's been an empty constructor on ServiceState which is public. So if you want an instance of it, all you have to do is:
ServiceState serviceState = new ServiceState();
编辑:
现在,我理解你的处境,我能想到的唯一的事情是,你注册一个PhoneStateListener后,您将获得在PhoneState更新(它可能还没有真正改变,但触发一个事件,让你知道当前值我猜)。所以,我能想到的唯一的解决方法将是保持当你想知道的当前值登记phoneStateListener并注销它时,你不需要它了。
Now that I understand your situation, the only thing I can think of is that after you register a PhoneStateListener you will get an update on the PhoneState (it may not have actually changed but an event is fired to let you know the current value I guess). So the only workaround I can think of would be to keep registering a phoneStateListener when you want to know the current value and deregistering it when you don't need it anymore.
希望帮助
这篇关于如何获取ServiceState的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!