Tuesday, May 24, 2016

Check if your Android app is in foreground or background


Now I have my Own Blog. Follow Link to learn more about Android Development.


In this example I will explain how to check if your Android Application is running in foreground or it is in background.

Note: we do not need to add any permission for this code. we will be using ActivityManager class
and its method getRunningAppProcesses()
getRunningAppProcesses() Returns a list of application processes that are running on the device.

Also I am using AsyncTask because we should not call ActivityManager.getRunningAppProcesses()
from UI thread it will return importance IMPORTANCE_FOREGROUND for your task no matter whether it is actually in the foreground or not.


 private class CheckIfForeground extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... voids) {

        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                Log.i("Foreground App", appProcess.processName);

                if (mContext.getPackageName().equalsIgnoreCase(appProcess.processName)) {
                    Log.i(Constants.TAG, "foreground true:" + appProcess.processName);
                    foreground = true;
                    // close_app();
                }
            }
        }
        Log.d(Constants.TAG, "foreground value:" + foreground);
        if (foreground) {
            foreground = false;
            close_app();
            Log.i(Constants.TAG, "Close App and start Login Activity:");

        } else {
            //if not foreground
            close_app();
            foreground = false;
            Log.i(Constants.TAG, "Close App");

        }

        return null;
    }
}
and execute AsyncTask like this.
new CheckIfForeground().execute();
Please Note this code is tested on Android Version 5.0.2


Author:
Hammad Tariq
Android Developer

6 comments:

  1. You completely match our expectation and the variety of our information.
    jantakhoj hyderabad

    ReplyDelete
  2. its working only for current application not for all installed app.

    ReplyDelete
  3. We are truly thankful for your blog entry. You will discover a great deal of methodologies in the wake of going to your post. I was precisely scanning for. A debt of gratitude is in order for such post and please keep it up.
    devops lead jobs

    ReplyDelete
  4. Hi, I find reading this article a joy. It is extremely helpful and interesting and very much looking forward to reading more of your work.. ecommerce development salt lake city

    ReplyDelete
  5. Please give some advice on how to achieve this kind of posts. this website

    ReplyDelete

Kotlin Android MVP Dagger 2 Retrofit Tutorial

http://developine.com/building-android-mvp-app-in-kotlin-using-dagger-retrofit/