【问题标题】:Determine if a Method is a `RemotableViewMethod`确定方法是否为 `RemotableViewMethod`
【发布时间】: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 访问。如何知道是否可以访问?

【问题讨论】:

标签: java android reflection remoteview


【解决方案1】:

通过写我的问题,我有了按名称查找课程的想法,并且它奏效了。 所以我将我的方法更新为以下内容:

private boolean canShowProgress(){
        LogCat.d(TAG, "canShowProgress");
        Class<ProgressBar> barclz = ProgressBar.class;
        try {
            Method method = barclz.getMethod("setVisibility", new Class[]{int.class});
            Class c = null;
            try {
                c = Class.forName("android.view.RemotableViewMethod");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return (this.showProgress= (c != null && method.isAnnotationPresent(c)));


        } catch (SecurityException e) {
            LogCat.stackTrace(TAG, e);
        } catch (NoSuchMethodException e) {
            LogCat.stackTrace(TAG, e);

        }
        return false;
    }

完美运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2014-04-09
    • 2018-02-21
    • 2013-02-17
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多