gpt4 book ai didi

android - 在 java android 中获取上下文

转载 作者:太空狗 更新时间:2023-10-29 15:35:27 26 4
gpt4 key购买 nike

嘿,我有一个 android 应用程序,我在其中监控后台线程中的 gmail 服务器。这个线程无限运行......在邮箱中收到邮件时,我必须干杯......但我不能获取当前 Activity 的上下文(因为应用程序中有多个 Activity )...如何获取当前正在运行的 Activity 的上下文或者可能是应用程序..getApplicationcontext 给出 null。

最佳答案

如果您正在运行一个Activity 或一个Service,它们本身都是 Context,所以调用这个就足够了。在监听器的任何回调方法中,this 将引用监听器,而不是 Activity。您可以使用任何 View 引用 Context:

Context context = myView.getContext();  

关于android - 在 java android 中获取上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5919184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com