Need Context when no activity is running

前端 未结 5 520
离开以前
离开以前 2021-01-26 08:16

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

5条回答
  •  不要未来只要你来
    2021-01-26 09:15

    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.

    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.

    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.

提交回复
热议问题