0

Is there any way to detect an app is running on screen? This means the app the user is working with and is visible to the user. Thanks in advance!

4

3 回答 3

1

The previous answer is a good general purpose solution but does require the additional permission and may not exactly handle your requirement for knowing if something is visible. If youre looking to do this from inside your own app, an alternative might be to track the visibility of your activities using the activity lifecycle Android provides.

Note that Activity.onPause happens when activity is about to become no longer visible to user. (from: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle)

It's tempting to use onStop to detect non-visibility, but this is often fired AFTER the next activity is already visible.

Conversely, onResume can be used to track that an activity is visible.

You could use these behaviors via a subclassing of Activity along with an implementation of android.app.application to keep track of whether or not any of your activities are visible.

For example:

The Application class used to do tracking:

public class Application extends android.app.Application {
    private static final String TAG = "example";

    private Class<? extends BaseActivity> _visibleActivity = null;

    public boolean isAnyActivityVisible() {
    return (_visibleActivity != null);
    }

    public synchronized void setVisibleActivity(Class<? extends BaseActivity> activity) {
    _visibleActivity = activity;

    Log.w(TAG,
        String.format("setting visible activity to: %s",
            isAnyActivityVisible() ? _visibleActivity.toString() : "none")
    );
    }
}

To use this adjust the application tag in your manifest to reference your Application class as in:

<application
        android:name="com.example.activityvisibletracker.Application" />
...
</application>

The BaseActivity that all of your Activities will derive from:

public abstract class BaseActivity extends Activity {

    @Override
    public void onResume(){
    super.onResume();
    Application app = (com.example.activityvisibletracker.Application) this.getApplication();
    app.setVisibleActivity(this.getClass());
    }

    @Override
    public void onPause(){
    super.onPause();

    Application app = (Application) this.getApplication();
    app.setVisibleActivity(null);
    }
}

And finally, an example Activity that extends BaseActivity:

public class MainActivity extends BaseActivity {
    // whatever implementation you like
}

Whenever you want to know if something is visible (probably from some service or thread in your app) you only need a Context to get at the Application and then can call isAnyActivityVisible() as in:

// cast getApplication() result to your Application type
((com.example.activityvisibletracker.Application)context.getApplicationContext()).isAnyActivityVisible();
于 2012-08-24T19:00:35.927 回答
0

Yes. Check this code:

    List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(4096); 
    return runningTasks.get(0).topActivity.getPackageName();

returns the package name. This needs

    <uses-permission android:name="android.permission.GET_TASKS" />
于 2012-08-24T17:45:12.930 回答
0

This doesn't work when the lockscreen is called on ICS, you get the whole lifecycle even though your app just remained sitting there.

于 2012-09-10T09:21:31.993 回答