请查看以下代码,我不明白我错在哪里。我需要在 Broadcastreciever 中知道应用程序是在前台还是在后台。但在接收器中它总是返回 FALSE。为什么 BASEAPPLICATION 中的静态变量的值丢失了?为什么它总是显示 FALSE
public class Main extends Activity {
@Override
protected void onResume() {
super.onResume();
BaseApplication.activityResumed();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Recevier.class);
PendingIntent pi = PendingIntent.getBroadcast(context, intentCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentMilliSeconds() + 2000, pi);
}
protected void onStop() {
super.onStop();
BaseApplication.activityPaused();
}
广播接收器
@Override
public void onReceive(Context context, Intent intent) {
if(BaseApplication.isActivityVisible()){
// Application is Running
}
else{
// Applicaiton is not Running
//**ALWAYS GIVING FALSE**
}
}
*基础应用 *
public class BaseApplication {
public static boolean isActivityVisible() {
return activityVisible;
}
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
private static boolean activityVisible;
}