Please review following code, i am not understanding where i am WRONG. I need to know in Broadcastreciever whether APPLICATION IS AT FOREGROUND or in BACKGROUND. But in Recevier it always Returning FALSE. Why the value of static variable in BASEAPPLICATION lost?? Why its Showing FALSE always
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();
}
BROADCAST RECIEVER
@Override
public void onReceive(Context context, Intent intent) {
if(BaseApplication.isActivityVisible()){
// Application is Running
}
else{
// Applicaiton is not Running
//**ALWAYS GIVING FALSE**
}
}
* BASE APPLICATION *
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;
}
The problem is the application is not aware of your BaseApplication class.
You should:
Your base application class should extend Application object
Your manifest should point out in the application
tag the attribute name
and point to you application object
PS the BaseApplication.activityPaused();
should go into onPause
and not onStop
如果要保持“ activityVisible”状态,则需要将其保存在SharedPreferences中。
You had better to check the top activity of activity stack.
public boolean isActivityVisible(Context context) {
ActivityManager am = (ActivityManager) context.
getSystemService(Activity.ACTIVITY_SERVICE);
String className = am.getRunningTasks(1).get(0).topActivity.getClassName();
return Main.class.getName().equals(className);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.