簡體   English   中英

我怎么知道一個android app小部件是否在后台

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

我不想在進入后台時更新我的​​應用程序小部件的用戶界面(您看不到它),我怎么知道應用程序小部件在后台?

您無法檢測您的主屏幕小部件當前是否可見。 有一個開放的bug報告請求這個位置

充其量,您可以檢查主屏幕(或通常的任何啟動器)是否可見:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM