Check if Activity is running from Service

前端 未结 8 418
星月不相逢
星月不相逢 2020-12-02 22:29

How can a Service check if one of it\'s application\'s Activity is running in foreground?

相关标签:
8条回答
  • 2020-12-02 22:52

    Use SharedPreferences to save the status of your app in onResume, onPause etc.

    like so:

     @Override
    public void onPause() {
        super.onPause();
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isActive", false).commit();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isActive", false).commit();
    }
    
    @Override
    public void onResume() {
        super.onResume();
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("isActive", true).commit();
    }
    

    and then in the service:

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isActive", false)) {
                return;
    }
    

    i used both onPause and onDestroy because sometimes it jumps straight to onDestroy:) it's basically all voodoo

    anyway, hope that helps someone

    0 讨论(0)
  • 2020-12-02 22:53

    There is one flaw to most of the answers above, if your activity has some feature which triggers another activity over the top, e.g. sending an email

    enter image description here

    the topActivity will not return your package name but instead the Android activity selector package name.

    Thus, it is better to check for the baseActivity instead of the topActivity.

    public boolean isMainActivityRunning(String packageName) {
        ActivityManager activityManager = (ActivityManager) getSystemService (Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(Integer.MAX_VALUE); 
    
        for (int i = 0; i < tasksInfo.size(); i++) {
            if (tasksInfo.get(i).baseActivity.getPackageName().toString().equals(packageName)
                return true;
        }
    
        return false;
    } 
    
    0 讨论(0)
  • 2020-12-02 22:55

    Use the below method to get full class name (package+class name) of the current activity and check if it is equal to full class name (package+class name) of activity you want:

     public String getCurrentClass() {
    
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
    
        ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
        String className = componentInfo.getClassName();
        return className;
     }
    
    0 讨论(0)
  • 2020-12-02 22:57

    Erian has the correct answer, but its not safe to use "getDefaultSharedPreferences". When you start a Service it use a differente instance that the Activity. Any changes of preferences in the activity, doesnt update the default shared preferences in the Service. So i will change Erian code with ".getSharedPreferences" like this:

    In Activity:

        @Override
        public void onPause() {
            super.onPause();
            getApplicationContext().getSharedPreferences("preferences", MODE_MULTI_PROCESS).edit().putBoolean("isActive", false).commit();;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            getApplicationContext().getSharedPreferences("preferences", MODE_MULTI_PROCESS).edit().putBoolean("isActive", false).commit();
        }
    
        @Override
        public void onResume() {
            super.onResume();
            getApplicationContext().getSharedPreferences("preferences", MODE_MULTI_PROCESS).edit().putBoolean("isActive", false).commit();
        }
    

    In Service:

        if (getApplicationContext().getSharedPreferences("preferences", MODE_MULTI_PROCESS).getBoolean("isActive", false)) {
            return;
        }
    
    0 讨论(0)
  • 2020-12-02 22:59

    Use the below method with your package name. It will return true if any of your activities is in foreground.

    public boolean isForeground(String myPackage) {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); 
        ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
        return componentInfo.getPackageName().equals(myPackage);
    }
    

    Update:

    Add Permission:

    <uses-permission android:name="android.permission.GET_TASKS" />
    
    0 讨论(0)
  • 2020-12-02 23:00

    Starting from Lollipop, getRunningTasks is deprecated:

     * <p><b>Note: this method is only intended for debugging and presenting
     * task management user interfaces</b>.  This should never be used for
     * core logic in an application, such as deciding between different
     * behaviors based on the information found here.</p>
     *
     * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
     * is no longer available to third party applications.
    

    One way to do this is to bind to the service on app start. Then: 1. If you need to check any of the app's activity is running, you can create a base class for your activities and override onPause and onResume. In onPause, call a service method to let it know it is on the background. In onResume, call a service method to let it know it is on the foreground. 2. If you only need to do this on some specific activity, just override onResume and onPause on those activities or create a base activity for those activities.

    0 讨论(0)
提交回复
热议问题