title: how to cast context for custom Application class
body: <p>I am developing the logic to check whether my app is in foreground with custom Application class.  When I try to cast the class in MyFirebaseMessagingService.java, the error comes out.  The code is shown below.</p>
  
  <pre><code>boolean isForeground = ((MyAppStatus)getApplication()).isForeground();
  </code></pre>
  
  <p>Also, I tried with codes like below.</p>
  
  <pre><code>boolean isForeground = MyAppStatus.get(getApplicationContext).isForeground();
  </code></pre>
  
  <p>and</p>
  
  <pre><code>boolean isForeground = MyAppStatus.get(getApplication).isForeground();
  </code></pre>
  
  <p>But, I encountered the same cast error.</p>
  
  <p>The code of the custom Application class is shown below.</p>
  
  <pre><code>public class MyAppStatus extends Application {
      private AppStatus mAppStatus = AppStatus.FOREGROUND;
  
      public void onCreate() {
          super.onCreate();
  
          registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
      }
  
      public static MyAppStatus get(Context context) {
          return (MyAppStatus)context.getApplicationContext();
      }
  
      public AppStatus getAppStatus() {
          return mAppStatus;
      }
  
      // check if app is foreground
      public boolean isForeground() {
          return mAppStatus.ordinal() &gt; AppStatus.BACKGROUND.ordinal();
      }
  
      public enum AppStatus {
          BACKGROUND,                // app is background
          RETURNED_TO_FOREGROUND,    // app returned to foreground(or first launch)
          FOREGROUND;                // app is foreground
      }
  </code></pre>
  
  <p>The error is like below.</p>
  
  <pre><code>12-10 22:26:59.256 32544-32633/com.forwards.android.goodhus E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-1
                                                                            Process: com.XXXXXX.android.XXXXXX, PID: 32544
                                                                            java.lang.ClassCastException: android.app.Application cannot be cast to com.XXXXXX.android.XXXXXX.common.utils.MyAppStatus
                                                                                at com.XXXXXX.android.XXXXXX.common.fcm.MyFirebaseMessagingService.sendToNotificationCenter(MyFirebaseMessagingService.java:187)
  </code></pre>
  
  <p>Some with great experience would be great help.
  Thanks a lot in advance.</p>