サーバーのフラグを定期的にチェックするアプリケーションがあります。そして、このフラグの値に応じてメッセージを表示します。
メッセージを表示したくない場合、アプリケーションは前面にありません。また、SharedPreferences を使用して、アプリケーションの状態を手動で保存します。各アクティビティで、次のようなことを行います。
@Override
protected void onStart() {
super.onStart();
SharedPreferences.Editor prefs = context.getSharedPreferences("myprefs", getApplicationContext().MODE_PRIVATE).edit();
prefs.putBoolean("appInFront", true);
prefs.commit();
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor prefs = context.getSharedPreferences("myprefs", getApplicationContext().MODE_PRIVATE).edit();
prefs.putBoolean("appInFront", false);
prefs.commit();
}
これにより、「appInFront」設定からアプリケーションの状態を取得できます。
SharedPreferences prefs = context.getSharedPreferences("myprefs", Context.MODE_PRIVATE);
boolean appInFront = prefs.getBoolean("appInFront", true);
しかし、アプリケーションの現在の状態を取得するためのネイティブ メソッドまたは方法が存在する可能性があります (アプリケーションは前面にあるのか、それともバックグラウンドにあるのか)。