2013-07-26 1 views
2

리플렉션을 사용하여 Android 기기의 라디오 버전을로드하려고합니다. 내 SDK는 API (7)에 다시 지원하지만 Build.RADIO이 API 8에 첨가하고, Build.getRadioVersion은() API에 추가 된 때문에이 작업을 수행해야 14NoSuchMethodException로드 리플렉션을 사용하여 Build.getRadioVersion()

// This line executes fine, but is deprecated in API 14 
String radioVersion = Build.RADIO; 

// This line executes fine, but is deprecated in API 14 
String radioVersion = (String) Build.class.getField("RADIO").get(null); 

// This line executes fine. 
String radioVersion = Build.getRadioVersion(); 

// This line throws a MethodNotFoundException. 
Method method = Build.class.getMethod("getRadioVersion", String.class); 
// The rest of the attempt to call getRadioVersion(). 
String radioVersion = method.invoke(null).toString(); 

아마 뭔가 잘못하고 있어요 이리. 어떤 아이디어?

답변

1

이 시도 :

Build.getRadioVersion()이 실제로 무엇을
try { 
    Method getRadioVersion = Build.class.getMethod("getRadioVersion"); 
    if (getRadioVersion != null) { 
     try { 
      String version = (String) getRadioVersion.invoke(Build.class); 
      // Add your implementation here 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } else { 
     Log.wtf(TAG, "getMethod returned null"); 
    } 
} catch (NoSuchMethodException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

실수로 String 유형 매개 변수를 지정했습니다. 도와 주셔서 감사합니다. –

1

gsm.version.baseband 시스템 프로퍼티의 값을 반환합니다. 이 속성이 available even in API 4입니다 AndroidXref에 따르면

static final String PROPERTY_BASEBAND_VERSION = "gsm.version.baseband"; 

public static String getRadioVersion() { 
    return SystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION, null); 
} 

: BuildTelephonyProperties 소스를 확인합니다. 따라서 리플렉션을 사용하여 SystemProperties을 통해 모든 Android 버전에서 사용할 수 있습니다.

public static String getRadioVersion() { 
    return getSystemProperty("gsm.version.baseband"); 
} 


// reflection helper methods 

static String getSystemProperty(String propName) { 
    Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties"); 
    Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class); 
    return tryInvoke(mtdGet, null, propName); 
} 

static Class<?> tryClassForName(String className) { 
    try { 
    return Class.forName(className); 
    } catch (ClassNotFoundException e) { 
    return null; 
    } 
} 

static Method tryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) { 
    try { 
    return cls.getDeclaredMethod(name, parameterTypes); 
    } catch (Exception e) { 
    return null; 
    } 
} 

static <T> T tryInvoke(Method m, Object object, Object... args) { 
    try { 
    return (T) m.invoke(object, args); 
    } catch (InvocationTargetException e) { 
    throw new RuntimeException(e.getTargetException()); 
    } catch (Exception e) { 
    return null; 
    } 
} 
+0

고맙습니다. 제가 확인하겠습니다. –

관련 문제