Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know in BroadcastReceiver if App is running on foreground?

I am working in application that needs make a synchronization every night. I use Alarm Manager that calls a BroadcastReceiver at the hour that I want. The problem is that I cant make a synchronization if the application is running in foreground to avoid losing data. So I need to know in Broadcast Receiver if the app is running in foreground to cancel this synchronization.

I tried solutions that I found in StackOverflow: Checking if an Android application is running in the background But this parameter is always false in BroadcastReceiver, but true in activites.

Can anyone tell me which is the problem? What am I doing bad?

Really thanks!

like image 245
pgarriga Avatar asked May 09 '14 10:05

pgarriga


People also ask

How do you know if an app moves to the background or foreground?

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

What does it mean app running in foreground?

Running in the Foreground means your app is currently Fully Visible on your device, you can see it and interact with it and it will respond to you right away. Follow this answer to receive notifications.

How do I start foreground service in broadcast receiver?

First, you need to declare two permissions in the manifest file. This permission is required for running the foreground permission which tells that your app going to run the foreground service. If you want to read in detail about this service then you can follow this link.

Does broadcast receiver run in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.


2 Answers

Try this way hope this works for you

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (isAppForground(context)) {
            // App is in Foreground
        } else {
            // App is in Background
        }
    }

    public boolean isAppForground(Context mContext) {

        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
                return false;
            }
        }

        return true;
    }

}

Add this permission

<uses-permission android:name="android.permission.GET_TASKS" />
like image 123
Biraj Zalavadia Avatar answered Oct 04 '22 04:10

Biraj Zalavadia


What do you mean by "the application is running in foreground"?

If you mean there is an Activity currently displayed on the screen, then the easiest way would be to make a base Activity class that sets a global boolean in your `Application' class.

Custom Application class:

public class MyApp extends Application
{
    public boolean isInForeground = false;
}

Custom base Activity class:

abstract public class ABaseActivity extends Activity
{

    @Override
    protected void onResume()
    {
        super.onResume();

        ((MyApp)getApplication()).isInForeground = true;
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        ((MyApp)getApplication()).isInForeground = false;
    }
}

I assume you are not synchronising from your BroadcastReceiver - you should instead be launching a Service to do the synchronisation. Otherwise the system might kill your app - you must not be doing any long-running tasks in a BroadcastReceiver.

So before you launch your sync service, check the application boolean to see if your app is "in foreground". Alternatively, move the check inside the sync service, which has the advantage of making the BroadcastReceiver even simpler (I am always in favour of trying to make the receivers have as little logic as possible).


This method has the advantages that it is simple to use, understand, and requires no extra permissions.

like image 21
Richard Le Mesurier Avatar answered Oct 04 '22 04:10

Richard Le Mesurier