Issue
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.
boolean isForeground = ((MyAppStatus)getApplication()).isForeground();
Also, I tried with codes like below.
boolean isForeground = MyAppStatus.get(getApplicationContext).isForeground();
and
boolean isForeground = MyAppStatus.get(getApplication).isForeground();
But, I encountered the same cast error.
The code of the custom Application class is shown below.
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() > AppStatus.BACKGROUND.ordinal();
}
public enum AppStatus {
BACKGROUND, // app is background
RETURNED_TO_FOREGROUND, // app returned to foreground(or first launch)
FOREGROUND; // app is foreground
}
The error is like below.
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)
Some with great experience would be great help. Thanks a lot in advance.
Solution
You need to register your MyAppStatus
class in manifest application
tag, like
<application
android:name=".MyAppStatus"
Answered By - Pavneet_Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.