How to check If I am currently on home screen

2020-06-25 04:53发布

Is this possible to check if currently my app is in background and home screen is launched

标签: android
4条回答
欢心
2楼-- · 2020-06-25 05:08
if (apps.get(0).topActivity.getPackageName().equals("com.android.launcher")

this somehow solved my problem only for default home

查看更多
混吃等死
3楼-- · 2020-06-25 05:13

Wether use onPause/onStop and onResume methods for activity purposes, or make advantage with an own implemented service for backround-processing (based on time-delays or messages/receivers).

查看更多
乱世女痞
4楼-- · 2020-06-25 05:21

There is no API to know whether the home screen is showing. However you can know when your app is sent to the background using the various Activity lifecycle callbacks (onStop, etc.)

查看更多
叼着烟拽天下
5楼-- · 2020-06-25 05:22

Try this function,

public boolean isUserIsOnHomeScreen() {
    ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
    for (RunningAppProcessInfo process : processes) {
        if(process.pkgList[0].equalsIgnoreCase("com.android.launcher")) {
            return true;
        } else {
            return false;
        }           
    }
    return false;
}
查看更多
登录 后发表回答