如何获取从广播接收器应用程序上下文? [英] How to obtain application context from broadcast receiver?
问题描述
我有我需要获取我创建一个BroadcastReceiver参考Application类的扩展。传递到的onReceive的上下文中是受限制的情况下。有没有一种方法来获得引用到实际的应用程序上下文?
I have an extension of the Application class that I need to obtain reference in a BroadcastReceiver I have created. The context passed into the "onReceive" is a restricted context. Is there a way to obtain reference to the actual application context?
推荐答案
呼叫 getApplicationContext()
在上下文
在的onReceive()
提供给您的,就像你会打电话 getApplicationContext()
上的活动
。
Call getApplicationContext()
on the Context
supplied to you in onReceive()
, just as you would call getApplicationContext()
on an Activity
.
我的应用程序的扩展,让我无静态获取引用我需要几个对象。
I have an extension of the Application that allows me to non-statically obtain reference to several objects I need.
虽然语法应用程序
不是一成不变的,它具有相同的影响,特别是对于内存泄漏。
While syntactically Application
is not static, it has the same impact, particularly with respect to memory leaks.
这篇关于如何获取从广播接收器应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!