【发布时间】:2012-09-05 11:43:03
【问题描述】:
我正在构建一个包含 ProgressBar 的小部件。如果 Widget 正在计算,我将 ProgressBar 的可见性设置为 VISIBLE,如果所有计算都停止,则设置为 INVISIBILE。应该没有问题,因为setVisibility 记录为RemotableViewMethod。然而,HTC 的一些人似乎忘记了它,(即在 Wildfire S 上),所以调用 RemoteViews.setVisibility 会导致崩溃。出于这个原因,我尝试实施检查,如果 setVisibility 真的可以调用。我已经为它写了这个方法:
private boolean canShowProgress(){
LogCat.d(TAG, "canShowProgress");
Class<ProgressBar> barclz = ProgressBar.class;
try {
Method method = barclz.getMethod("setVisibility", new Class[]{int.class});
Annotation[] anot = method.getDeclaredAnnotations();
return anot.length > 0;
} catch (SecurityException e) {
LogCat.stackTrace(TAG, e);
} catch (NoSuchMethodException e) {
LogCat.stackTrace(TAG, e);
}
return false;
}
这会起作用,但 真的 丑陋,因为如果存在 ANY 注释,它将返回“True”。我看了看,RemoteView 本身是如何进行查找的,发现了这个:
if (!method.isAnnotationPresent(RemotableViewMethod.class)) {
throw new ActionException("view: " + klass.getName()
+ " can't use method with RemoteViews: "
+ this.methodName + "(" + param.getName() + ")");
}
但我不能这样做,因为 RemotableViewMethod 类无法通过 sdk 访问。如何知道是否可以访问?
【问题讨论】:
-
嘿!你认为这是你可能能够回答的问题吗? stackoverflow.com/questions/36679333/…
标签: java android reflection remoteview