隨筆雜記

             :: 首頁 :: 新隨筆 ::  ::  :: 管理 ::
          使用:
          (Boolean) invokeMethod(LockPatternUtils.class, mLockutils,
                              "savedPasswordExists", new Class[] {int.class}, new Object[] {UserHandle.myUserId()});

          (Boolean) invokeMethod(LockPatternUtils.class, mLockutils, "checkPattern",
                              new Class[] {List.classint.class}, new Object[] {null, UserHandle.myUserId()});

          方法實現:

              public static Method getMethod(Class<?> cls, String methodName, Class<?> parameterTypes) {
                  try {
                      return cls.getDeclaredMethod(methodName, parameterTypes);
                  } catch (NoSuchMethodException e) {
                      Log.e(TAG, "getMethod() Exception: ", e);
                      try {
                          return cls.getMethod(methodName, parameterTypes);
                      } catch (NoSuchMethodException ex) {
                          Log.e(TAG, "getMethod() Exception: ", ex);
                          return null;
                      }
                  }
              }

              public static Object invokeStaticMethod(Class<?> cls, String methodName) {
                  return invokeMethod(cls, null, methodName, nullnull);
              }

              public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?>[] parasTypes,
                      Object[] parasObjs) {
                  return invokeMethod(cls, null, methodName, parasTypes, parasObjs);
              }

              public static Object invokeMethod(Class<?> cls, Object obj, String methodName) {
                  return invokeMethod(cls, obj, methodName, nullnull);
              }

              public static Object invokeMethod(Class<?> cls, Object obj, String methodName, Class<?>[] parasTypes,
                      Object[] parasObjs) {
                  Method method = getMethod(cls, methodName, parasTypes);
                  try {
                      if (method != null) {
                          method.setAccessible(true);
                          return method.invoke(obj, parasObjs);
                      }
                  } catch (Exception e) {
                      Log.e(TAG, "invokeStaticMethod() Exception: ", e);
                  }
                  return null;
              }
              
          posted on 2015-12-18 09:42 天宇恒星 閱讀(260) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 德惠市| 攀枝花市| 白银市| 和龙市| 德格县| 重庆市| 隆化县| 苍南县| 玉溪市| 武威市| 高邑县| 饶阳县| 信丰县| 布尔津县| 淮北市| 江永县| 柘城县| 衡阳县| 盐亭县| 徐闻县| 黄冈市| 宜州市| 邳州市| 和田县| 安福县| 潞城市| 施秉县| 睢宁县| 礼泉县| 乌拉特前旗| 栾城县| 兖州市| 甘德县| 三明市| 什邡市| 阿克苏市| 井冈山市| 高州市| 徐闻县| 渝中区| 江达县|