Reputation: 990
I am working on a chat app. I am retrieving chats directly from the server. I am listening for new nodes. So, If, the app is in foreground and the notification is from the same user with whom I am talking. I don't want to show the notification. How can I check in Service which Activity is running in foreground and content of that activity.
Upvotes: 0
Views: 139
Reputation: 803
You can use SharedPreferences to save current running activity of your app in onResume, onPause.
like below:
@Override
public void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isCurrent", false).commit();
}
@Override
public void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isCurrent", false).commit();
}
@Override
public void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isCurrent", true).commit();
}
and then in your service:
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isCurrent", false)) {
return;
}
Upvotes: 1