Determine if the application in the foreground is frowned?

There are many reasons why foreground application discovery. for example, as a trigger for push notification GCM / C2DM - many applications will have a good reason for implementing different behaviors when the application is front and rear. Another reason may be that services that consume valuable resources, such as, for example, server requests in a background task, can be destroyed.

Just to be clear: definition (as I see) for a background application :
An application in which none of its actions is called by the onStart() method and does not yet call the onStop() method. that since activity is visible to the user in this life cycle only at this time.

On the other hand -

  • Google doesn't seem to want the app to respond to the home button (this is not part of the API)

  • reacts to onBackPressed() to "root / main" activity as an indicator of the Activity exit, of course, not a good idea (because many users use the home button and not the back button)

  • there is no method in the API to determine if an application is a priority (according to my definition ..)

if I have not missed something in the API, and it really is - Why is there no reason to easily determine if the application is in the foreground or not ???? !!!!

what I know, I can do to determine if the application is foreground in this thread - How to determine when an Android application goes to the background and comes back to the fore

but as @Emil says it requires special permission or requires some complex logic that quickly becomes problematic to maintain, and it smells like a bad approach (although what I'm doing now because I don’t have a better idea ...)

my questions are mostly :

  • Is there an API method for a good reason?

  • Is the application in the foreground or not - a bad approach?

  • Is there any other way to find out if an application is prioritized or not?

+3
android activity-lifecycle application-lifecycle
source share
3 answers

takes into account whether the application is a priority or not is a bad approach?

Given the assumption in front of the background, reasonable.

is there any other way to find out if the application is prioritized or not?

You can roughly divide the scripts for this into two groups:

  • Cases in which you want to take action immediately after changing the state of the foreground / background

  • Cases when some other event AlarmManager ( AlarmManager , incoming system, etc.), and at this moment you want to take different actions based on whether you are in the foreground

In the first case, onUserLeaveHint() is your most reliable simple option. I cannot guarantee that it will cover all cases, but it should handle the HOME script, for example. You can also save a reference account of running actions in a static data element and try to use it instead.

In the latter case, streamlined translation may be useful.

+5
source share

I had the same problem. I want to display a push notification when my activity is not in foreground mode. Read the following code and you will get a response.

  Context ctx = context.getApplicationContext(); ActivityManager am = (ActivityManager) context .getSystemService(ACTIVITY_SERVICE); // get the info from the currently running task List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); PackageManager pm = this.getPackageManager(); try { /** * take fore ground activity name */ ComponentName componentInfo = taskInfo.get(0).topActivity; if (printLog == true) { Log.d("Home", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()); Log.d("Home", "Number Of Activities : " + taskInfo.get(0).numRunning); Log.d("Home", "Componenet Info : " + componentInfo.getPackageName()); Log.d("Home", "Componenet Info : " + componentInfo.getClassName()); } /** * All activities name of a package to compare with fore ground * activity. if match found, no notification displayed. */ PackageInfo info = pm.getPackageInfo( "<PackageName>", PackageManager.GET_ACTIVITIES); ActivityInfo[] list = info.activities; for (int i = 0; i < list.length; i++) { Log.d("TAG","Activity : "+list[i].name); } } catch (NameNotFoundException e) { e.printStackTrace(); } 

To use this, you must obtain permission in your manifest file.

uses-permission android: name = "android.permission.GET_TASKS"

Excuse me if I cannot answer your question.

0
source share

If you need to know if the application is in the background or in the foreground from a service running in the background (otherwise it does not make sense), you can just use the binding, that is, to bind it to all your onResume actions and undo all onPause actions. then in your service you can control not only the visibility of your application to the user, but also which of these actions are open at any time. it is also leakproof and more stable than a static variable (which can be cleaned up if necessary) since you use the android API and rely on the correctness of the Android OS code itself.

0
source share

All Articles