I have an Application class like so:
public class MyApplication extends Application {
private static boolean FOCUSED;
public static boolean isAppFocused() {
Log.v("@@@@@@ isAppFocused", "called");
return FOCUSED;
}
public static void appResumed() {
Log.v("@@@@@@ appResumed", "called");
FOCUSED = true;
}
public static void appPaused() {
Log.v("@@@@@@ appPaused", "called");
FOCUSED = false;
}
}
And inside my MainActivity I'm calling its methods like so:
@Override
protected void onResume() {
super.onResume();
MyApplication.appResumed();
}
@Override
protected void onPause() {
super.onPause();
MyApplication.appResumed();
}
As you see, I have never called the MyApplication.appPaused(); method. And FOCUSED variable's default value inside MyApplication is null.
Strangely when I call isAppFocused() inside service, it returns false!!!!!
I want to know if the app is focused so that it doesn't show notifications inside service. I even used SharedPreferences before this, and that had the same problem, no matter what, their value is not gonna be the one I expect. :|
I don't know why it doesn't want me to know if app has focus. :| I'm pissed off.
EDIT: I changed the Application class to this:
public class MyApplication extends Application {
private static String FOCUSED;
public static String isAppFocused() {
Log.v("@@@@@@ isAppFocused", "called");
return FOCUSED;
}
public static void appResumed() {
Log.v("@@@@@@ appResumed", "called");
FOCUSED = "true";
}
public static void appPaused() {
Log.v("@@@@@@ appPaused", "called");
FOCUSED = "false";
}
}
Now inside the Service I'm using it like so:
if(MyApplication.isAppFocused().equals("false")){
//show notifications
}
Now it throws NullPointerException, but I see in logcat that appResumed() method is called twice before calling isAppFocused().
inside AndroidManifest.xml:
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow" android:supportsRtl="true">
<!-- Splash screen -->
<activity
android:name="com.example.SplashScreen"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.example.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:launchMode="singleTask"
android:label="@string/app_name" >
</activity>
<!-- Login activity -->
<activity
android:name="com.example.Login"
android:screenOrientation="portrait">
</activity>
<!-- Push Service -->
<receiver android:name="com.example.service.MyBroadcast"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name="com.example.service.MyService"
android:process=":My_Service_Process"
/>
</application>
<application
android:name="com.example.MyApplication" >
</application>
EDIT 2: I changed inside MainActivity:
@Override
protected void onResume() {
super.onResume();
MyApplication.appResumed();
Log.v("**** isAppFocused", MyApplication.isAppFocused());
}
@Override
protected void onPause() {
super.onPause();
MyApplication.appPaused();
Log.v("**** isAppFocused", MyApplication.isAppFocused());
}
the result is correct, and onResume it returns "true" and onPause it returns "false". but inside the service it always returns the default value which is "true"; I don't know why it changes inside MainActivity but it doesn't change inside service.
Angry version of the Application class:
public class MyApplication extends Application {
private static String FOCUSED = "true";
public static String isAppFocused() {
Log.v("@@@@@@ isAppFocused", "called");
return FOCUSED;
}
public static void appResumed() {
Log.v("@@@@@@ appResumed", "called");
FOCUSED = "true";
}
public static void appPaused() {
Log.v("@@@@@@ appPaused", "called");
FOCUSED = "false";
}
}