如何检查Android应用程序是否未运行?

时间:2017-07-17 12:20:24

标签: android push-notification splash-screen

我的问题是我每次启动应用程序时都会运行SplashActivity 当应用程序在后台时收到通知我启动SplashActivity(它现在如何工作并且它可以工作,但它让人感觉每次用户响应通知时都会重新启动应用程序,并且会发生一些不必要的服务器更新)。

我想做的是:

if (app is in background) {
    if (running) {
        launch SomeActivity
    } else {
        launch SplashActivity
    }
}

我查了很多帖子,但没有一个能帮助我可靠地知道应用程序何时运行(我认为在后台运行时正在运行)。

我能够检测到应用程序处于前台或后台(这不是问题),但我的问题是检测到"已终止"状态。

我查看ActivityLifecycleCallbacks中的Application,但我无法看到它如何可靠地帮助我,因为无法保证调用onDestroy。

有人解决了这个问题吗?

由于

修改

我最终使用Google I/O 2016视频中建议的解决方案来检测背景和前景状态,并在onActivityDestroyed中添加了一小部分以检测我的主要活动onDestory

它并不完美,可以获得应用状态"如果主要标签活动的onDestory未被调用,则会出错,但这是我最终要做的事情:

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    public static final int APP_STATE_FOREGROUND = 1000;
    public static final int APP_STATE_BACKGROUND = 1001;
    public static final int APP_STATE_TERMINATED = 1002;

    private int mNumStarted;
    private int mAppState;

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
        // set default app state to APP_STATE_TERMINATED in case app state is not certain so app will launch as if it is a new app launch when opened from push notification
        mAppState = APP_STATE_TERMINATED; 

    }

    public int getAppState() {
        return mAppState;
    }

    /**
     * Application.ActivityLifecycleCallbacks interface methods
     */
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        String activityName = activity.getLocalClassName();
        if (activityName.equals("main.TabsActivity")) {
            mAppState = APP_STATE_FOREGROUND;
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (mNumStarted == 0) {
            mAppState = APP_STATE_FOREGROUND;
        }
        mNumStarted++;
    }

    @Override
    public void onActivityResumed(Activity activity) {}

    @Override
    public void onActivityPaused(Activity activity) {}

    @Override
    public void onActivityStopped(Activity activity) {
        mNumStarted--;
        if (mNumStarted == 0) {
            mAppState = APP_STATE_BACKGROUND;
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

    @Override
    public void onActivityDestroyed(Activity activity) {
        String activityName = activity.getLocalClassName();
        if (activityName.equals("main.TabsActivity")) {
            mAppState = APP_STATE_TERMINATED;
        }
    }
}

此解决方案适用于我(现在),也许它会帮助其他人。

2 个答案:

答案 0 :(得分:1)

方法1:您可以在发布应用程序中安全地使用以下方法。这些方法是不言自明的。但 isAppVisible()表示您的应用当前正在后台或前台运行,否则可能意味着应用程序被系统终止。如果它按预期工作,我也会为你调试一些日志。

public class MyApplication extends Application {

    private static final String TAG = MyApplication.class.getSimpleName();
    private int mVisibleCount;
    private boolean mInBackground;

    @Override public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override public void onActivityStarted(Activity activity) {
                mVisibleCount++;
                if (mInBackground && mVisibleCount > 0) {
                    mInBackground = false;
                    Log.i(TAG, "App in foreground");
                }
            }

            @Override public void onActivityResumed(Activity activity) {
            }

            @Override public void onActivityPaused(Activity activity) {
            }

            @Override public void onActivityStopped(Activity activity) {
                mVisibleCount--;
                if (mVisibleCount == 0) {
                    if (activity.isFinishing()) {
                        Log.i(TAG, "App is finishing");
                    } else {
                        mInBackground = true;
                        Log.i(TAG, "App in background");
                    }
                }
            }

            @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    public boolean isAppInBackground() {
        return mInBackground;
    }

    public boolean isAppVisible() {
        return mVisibleCount > 0;
    }

    public int getVisibleCount() {
        return mVisibleCount;
    }
}

方法2:还有另一种使用Service检测应用程序是否终止的方法。见link

答案 1 :(得分:0)

这里知道应用程序的方法是在后台还是没有

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