1
我有一个应用程序定期检查服务器的一些标志。 然后根据此标志的值显示一条消息。有没有办法获得应用程序的当前状态?
我不想显示消息,那么应用程序不在前面。 我使用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);
但可能存在本地方法或方式来获得应用程序的当前状态(应用程序是在前台还是在后台)?
我在BroadcastReceiver中使用Toast Notifications,就像SDKDeveloper中的ApiDemos/app/AlarmController一样。 – Serg
谢谢赫尔曼,我这样做了。 – Serg
为什么在onResume方法中有super.onStart()? – maysi