Need Context when no activity is running

前端 未结 5 511
离开以前
离开以前 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 08:55

    You may create MyApplication class, inherited from the Application, then inside that class do the following:

    public class MyApplication extends Application {
        private static MyApplication instance;
    
        @Override
        public void onCreate() {
            super.onCreate();
            instance = this;
            .........
        }
    
        public static Context getContext() {
            return instance;
        }
    

    After that, you may use MyApplication.getContext() anywhere if you need a context and don't have an Activity lying around.

提交回复
热议问题