Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can call the method using reflection and fail gracefully in case of errors (like missing class or methods). See <a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.html" rel="noreferrer">java.lang.reflect</a></p> <p>Other option is to compile code in level 9 but surround with try/catch to catch errors that would arise from execution on lower level. It could be fairly error prone, though, and I'd think twice about doing it.</p> <hr> <p>Update</p> <p>Here is test code</p> <pre><code>public void onCreate(Bundle savedInstanceState) { try { // First we try reflection approach. // Expected result // in 2.3 we print some value in log but no exception // in 2.2 we print NoSuchMethodException // In both levels we get our screen displayed after catch Method m = SensorManager.class.getMethod("getAltitude",Float.TYPE, Float.TYPE); Float a = (Float)m.invoke(null, 0.0f, 0.0f); Log.w("test","Result 1: " + a); } catch (Throwable e) { Log.e("test", "error 1",e); } try { // Now we try compiling against 2.3 // Expected result // in 2.3 we print some value in log but no exception // in 2.2 we print NoSuchMethodError (Note that it is an error not exception but it's still caught) // In both levels we get our screen displayed after catch float b = SensorManager.getAltitude(0.0f, 0.0f); Log.w("test","Result 2: " + b); } catch (Throwable e) { Log.e("test", "error 2",e); } super.onCreate(savedInstanceState); setContentView(R.layout.main); } </code></pre> <p>Results:</p> <p>2.3</p> <pre><code>09-14 07:04:50.374: DEBUG/dalvikvm(589): Debugger has detached; object registry had 1 entries 09-14 07:04:50.924: WARN/test(597): Result 1: NaN 09-14 07:04:51.014: WARN/test(597): Result 2: NaN 09-14 07:04:51.384: INFO/ActivityManager(75): Displayed com.example/.MyActivity: +1s65ms </code></pre> <p>2.2</p> <pre><code>09-14 07:05:48.220: INFO/dalvikvm(382): Could not find method android.hardware.SensorManager.getAltitude, referenced from method com.example.MyActivity.onCreate 09-14 07:05:48.220: WARN/dalvikvm(382): VFY: unable to resolve static method 2: Landroid/hardware/SensorManager;.getAltitude (FF)F 09-14 07:05:48.220: DEBUG/dalvikvm(382): VFY: replacing opcode 0x71 at 0x0049 09-14 07:05:48.220: DEBUG/dalvikvm(382): VFY: dead code 0x004c-0064 in Lcom/example/MyActivity;.onCreate (Landroid/os/Bundle;)V 09-14 07:05:48.300: ERROR/test(382): error 1 java.lang.NoSuchMethodException: getAltitude at java.lang.ClassCache.findMethodByName(ClassCache.java:308) </code></pre> <p>Skipped stack trace</p> <pre><code>09-14 07:05:48.300: ERROR/test(382): error 2 java.lang.NoSuchMethodError: android.hardware.SensorManager.getAltitude at com.example.MyActivity.onCreate(MyActivity.java:35) </code></pre> <p>Skipped more stack trace</p> <pre><code>09-14 07:05:48.330: DEBUG/dalvikvm(33): GC_EXPLICIT freed 2 objects / 64 bytes in 180ms 09-14 07:05:48.520: INFO/ActivityManager(59): Displayed activity com.example/.MyActivity: 740 ms (total 740 ms) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload