Question

I have an appwidget at HOME and is updated periodically. I want to stop the update if HOME is not visiable. I use a service to register a screen on/off receiver so as to start/stop updating the appwidget when the screen is on/off. But I don't know how to detect if the screen is on but the user is focusing on something else (i.e. HOME not at foreground). How can I detect if the user switch from/to HOME in my service or receiver (it's an appwidget and I don't have an activity on hand when it happens)?

Was it helpful?

Solution

As far as I know, there is no direct API to detect whether HOME is in foreground or background.

But the following snippet will tell you whether the user is currently at HOME or not.

if (mActivityManager.getRunningTasks().get(0).topActivity.getPackageName().equals("com.android.launcher") {

//home 

}

DISCLAIMER: The above code will work only for the default launcher (com.android.launcher). So, if the user is using some third-party launcher, you can't detect that if you don't know the package name of that launcher.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top