android - Is there a way to get a current state of the application? -


i have application periodically checks server flag. , displays message depending on value of flag.

i don't want display message application not in front. , use sharedpreferences store application state manually. in each activity like:

@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(); } 

and allows me state of application "appinfront" preference:

sharedpreferences prefs = context.getsharedpreferences("myprefs", context.mode_private); boolean appinfront = prefs.getboolean("appinfront", true);       

but may exists native method or way current state of application (is application in front or in background)?

what kind of message displaying?a notification or in activity? in application need state information?

you can write baseactivity , extend other activities from. need write less code. , counterpart of onpause() should use onresume():

public class baseactivity{  public static boolean appinfront;  @override protected void onresume() {     super.onresume();     appinfront = true; } @override protected void onpause() {     super.onpause();     appinfront = false; } 

}

with static public boolean can ask "anywhere" visibility state of app. don't need remember state between app restarts boolean sufficient.

if(baseactivity.appinfront){     //show message } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -