获取上下文的服务上下文

2023-09-12 07:14:28 作者:空港‘

有没有可靠的方式来获得上下文服务

Is there any reliable way to get a Context from a Service?

我想注册一个广播接收器 ACTION_PHONE_STATE_CHANGED ,但我不需要我的应用来一直得​​到这个信息,所以我不把它放在清单。但是,我不能有广播接收器 GC ,当我需要这些信息,所以我被杀害注册在服务的BR oadcast接收器 。因此,我需要一个上下文来调用 registerReceiver()。  当我不再需要 ACTION_PHONE_STATE_CHANGED 我注销它。

I want to register a broadcast receiver for ACTION_PHONE_STATE_CHANGED but I don't need my app to always get this information, so I don't put it in the Manifest. However, I can't have the broadcast receiver be killed by the GC when I need this information so I'm registering the broadcast receiver in a Service. Hence, I need a Context to to call registerReceiver(). When I no longer need the ACTION_PHONE_STATE_CHANGED I unregister it.

任何提示? 谢谢。

推荐答案

服务是一个上下文

 
精彩推荐