简体   繁体   中英

how can I know whether an android app widget is in background

我不想在进入后台时更新我的​​应用程序小部件的用户界面(您看不到它),我怎么知道应用程序小部件在后台?

You cannot detect whether or not your homescreen widget it currently visible. There is an open bug report requesting this here .

At best, you could check to see if the homescreen (or any launcher in general) is visible:

public boolean isHomeShowing(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    ArrayList<string> homeList = new ArrayList<string>();
    for(ResolveInfo info : this.getPackageManager().queryIntentActivities(intent, 0)){
        homeList.add(info.activityInfo.packageName);
    }

    ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningTaskInfo t : am.getRunningTasks(1)) {
            if (t != null && t.numRunning > 0) {
                ComponentName cn = t.baseActivity;
                if (cn == null)
                   continue;
                else
                   if (homeList.contains(cn.getPackageName())) return true;
            }
        }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM