android - How to check if an activity is running in background/foreground from a service? -


I have created a service that starts with the creation of an activity and closes on the activity's diastore method. Now I have to check with a method of service whether the activity is going on in the foreground / background (for some cases as if the application has been forced to shut down). How can I go about doing this?

  1. I need to do this cause, as far as I know there is no guarantee of any activity on the theft method, because of the accident, my service Which starts when my activity starts, will not stop after the event of any accident or any effective closure.

  2. I have seen where the activities of the foreground can be examined but I have to check the activity running in any situation (either foreground or background). I />

    Last update

    To check all activity:

      @override secured on zero attop () {super.onStop (); If (AppConstants.isAppSentToBackground (getApplicationContext ())) {what should be done only after the app is closed, close the session}}  

    The method for checking our apps is running Whether or not:

      Public stable boolean isAppSentToBackground (last reference reference) {try {ActivityManager am = (ActivityManager) reference .getSystemService (Context.ACTIVITY_SERVICE); // In the list of Running Tasks, first of all is foreground // work Runningtk Infine ForegroundTask Infofo = am.getRunningTasks (1) .get (0); String foreground taskPackageName = AggressiveInfo.activity.getPackageName (); // Get top foreground activity PackageManager pm = context.getPackageManager (); PackageInfo foreground app packageview = pm.getPackageInfo (foreground text package name, 0); String Foreground TaskAppName = Foreground AppPackageInfo.applicationInfo. LoadLabel (pm) .toString (); // Log.e ("", Foregroundmap AppName + "----------" + // Aggritix PackagesName); If (foreground TaskAppName.equals ("Your app name")) {return true; }} Hold (exception e) {Log.e ("isAppSentToBackground", "" + E); }     return false; }  

    Answer re-updated

    Use the method below with your package name. If you have any activity in the foreground, then this truth will return.

      is the foreground of the public boolean (string mypackage) {ActivityManager am = (ActivityManager) getSystemService (ACTIVITY_SERVICE); & Lt; ActivityManager.RunningTaskInfo & gt; RunningTaskInfo = am.getRunningTasks (1); Component component component = running TaskInfo.get (0) .topActivity; If (componentInfo.getPackageName (.) Equals (myPackage)) return true; return false; }  reply    

    Check this link first

    The life cycle of an Android application Details are there.

    Activity goes to background when paus () is called so you can disable update notifications in this method.

      Public stable boolean is ApplicationSentToBackground (last reference reference) {ActivityManager am = (ActivityManager) context.getSystemService (Context.ACTIVITY_SERVICE); & Lt; RunningTaskInfo & gt; Function = am.getRunningTasks (1); If (! Tasks.isEmpty ()) {ComponentName topActivity = taskss.get (0) .topActivity; If (! TopActivity.getPackageName (.) Equals (context.getPackageName ())) {back true; }     }      return false; }  

    Also include in the

      & lt; Usage-permission Android: name = "android.permission.GET_TASKS" />  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -