質問

I know that Android doesn't have an Application-level onPause the way an Activity has an onPause, but it looks like I need a similar functionality. I asked a (dumb) question awhile ago (http://stackoverflow.com/questions/9508301/checking-the-manner-in-which-an-activity-has-been-reanimated) and realized that onPause/onResume will give me what I'm looking for: The ability to know if the Activity has been relegated to the background (either via the Home button, opening a different app, the phone sleeping, etc.).

The trouble is, onPause also fires in an Activity as I go from one Activity in my App to another. So I'm heading down the road of finding EVERY SINGLE way a user can go from Activity to Activity in my app (and there are anywhere from 15 to 25 of those) and setting a flag right before each user-initiated Activity switch. Then in onResume I'm checking that flag and if it's X, I know that it was a user-initiated onResume. If the flag is Y, I know that it's a result of the Home button being pressed, the phone sleeping, etc.).

This just seems like a big messy kludge. Is there a better way?

(I add this to help, not to confuse: My goal is to have a (very annoying, I know) passcode screen that pops up any time the app goes away from the foreground for any reason (even screen sleep). I planned to use onPause/onResume to check when to pop the Passcode activity, but onPause fires every time I change Activities and therefore I need to add more.)

役に立ちましたか?

解決

Apps like Pinnacle Locker do something similar--I believe they intercept an Intent for specific packages, and thus are fired whenever the system switches to an app, but not when the user changes screens between apps.

I may be misunderstanding you, but another approach would be to turn your approach around: whenever you change from one screen to the other inside an app, you are either launching an Intent or finish()ing (such as when the user presses the back button), right?

Then you could set a flag in your internal Intents, and whenever you directly call finish() or when onBackPressed() is called in your apps you could set a similar flag. If that flag is not set when onResume() runs in your app, then you could show the lockscreen.

他のヒント

This doesn't really answer your question, but perhaps as an alternate approach, could you make an activity base class that does the flag management and have all your activities inherit from that? You could even do something like set a timeout of, say, 5 seconds between one activity's onPause and another activity's onResume to decide that you were doing an out-of-app switch instead of an in-app switch, although if you just have a utility function that you call from your activity base class for initiating an activity change, that still vastly reduces the surface area of where you have to keep track of things.

You should combine the onPause() function with application visibility status and process further if your application is not visible. Following code might help:

private boolean isVisible(String app) {
        ActivityManager activityManager = (ActivityManager) getApplicationContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                Log.i("Foreground App", appProcess.processName);
                if (app.equalsIgnoreCase(appProcess.processName)) {
                    return true;
                }
            }
        }

        return false;
    }

Again this might not fit in your current scenario entirely.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top