我有一个应用程序定期检查服务器是否有一些标志。然后根据此标志的值显示一条消息。
我不想显示一条消息,那么应用程序不在前面。我使用 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);
但是可能存在获取应用程序当前状态的本机方法或方式(应用程序是在前面还是在后台)?