我不想更新我的app小部件的UI到后台(你看不到它),我怎么知道app小部件在后台呢?
答案 0 :(得分: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;
}