获取服务中的上下文 [英] Get Context in a Service

查看:30
本文介绍了获取服务中的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何可靠的方法可以从 Service 获取 Context?

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

我想为 ACTION_PHONE_STATE_CHANGED 注册一个广播接收器,但我不需要我的应用程序总是获取这些信息,所以我没有把它放在 Manifest 中.

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.

但是,当我需要这些信息时,我不能让广播接收器被 GC 杀死,所以我在 Service 中注册了广播接收器.

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.

因此,我需要一个 Context 来调用 registerReceiver().当我不再需要 ACTION_PHONE_STATE_CHANGED 时,我取消注册.

Hence, I need a Context to to call registerReceiver(). When I no longer need the ACTION_PHONE_STATE_CHANGED I unregister it.

有什么建议吗?

推荐答案

Service 是一个 Context

这篇关于获取服务中的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆