问题描述
我从我的application.But的通知显示弹出没有任何活动是从我的应用程序运行在这样的时候通知我告诉对话,当我检查我的通知。对话抛出异常,因为没有活动运行和背景,我想过去对话constuctor为null,所以我需要一个背景下,当没有活动提前运行的感谢。或者有什么想法如何,我可以提前实现此感谢
I am displaying a pop up from Notification of my application.But when I check my notification no activity is running from my application so in notification when I show dialogue. Dialogue throws exception because no activity is running and context which I am passing to dialogue constuctor is null therefore I need a context when no activity is running thanks in advance. Or any Idea how I can implement this thanks in advance
推荐答案
我会假设你的code是在Android 服务
后台运行。如果是这样的话,你的服务
是上下文
本身,所以你可以使用它时,你需要一个。
I would assume that your code is running in a background in an Android Service
. If that's the case, your Service
is a Context
itself, so you can use it when you need one.
你的code是一个后台线程,而不是在一个Android 服务
(在这种情况下,您有其他大的问题)上运行,你可以用一个实例应用程序上下文的。你得到一个虽然叫 Context.getApplicationContext
,你可以得到它在活动启动你的背景code和缓存供以后。
Of your code is running in a background thread and not in an Android Service
(in which case you have other big problems), you could use an instance of the application context. You get one though calling Context.getApplicationContext
, and you can get it in the Activity
that starts your background code and cache it for later.
请注意,虽然有一定的陷阱,而不是活动的情况下使用应用程序上下文, - 例如,你不能使用 LayoutInflater
从应用程序上下文
Note though that there are certain gotchas with using application context, instead of activity context - for example, you can't use LayoutInflater
from application context.
这篇关于当没有活动运行需要上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!