【问题标题】:how can I know whether an android app widget is in background我怎么知道android应用小部件是否在后台
【发布时间】:2013-03-16 07:26:39
【问题描述】:

当我的应用小部件进入后台时,我不想更新它的 UI(你看不到它),我怎么知道应用小部件在后台?

【问题讨论】:

    标签: android android-appwidget


    【解决方案1】:

    您无法检测您的主屏幕小部件当前是否可见。有一个开放的错误报告请求此here

    您最多可以检查一下主屏幕(或任何一般的启动器)是否可见:

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

    【讨论】:

    • 非常感谢您的回答。
    猜你喜欢
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2017-03-14
    • 2023-02-10
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多