Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do that (checking the API version).</p> <p>You can also use reflection to call the newer classes.</p> <p>I wouldn't worry about using deprecated methods as all Android versions are backwards compatible, saying that you want to watch when things are for 3.0 Honeycomb as these are a little different.</p> <p>Here's an explanation of how to use reflection: (yes it's been on SO before, so maybe search for reflection)</p> <p><a href="http://www.youtube.com/watch?v=zNmohaZYvPw&amp;feature=player_detailpage#t=2087s">http://www.youtube.com/watch?v=zNmohaZYvPw&amp;feature=player_detailpage#t=2087s</a></p> <p>I'm looking at making the project this is in available but until then here's some code:</p> <p>(You could do this in a class that extends Application i.e. one time setup)</p> <pre><code> public static Method getExternalFilesDir; static { try { Class&lt;?&gt; partypes[] = new Class[1]; partypes[0] = String.class; getExternalFilesDir = Context.class.getMethod("getExternalFilesDir", partypes); } catch (NoSuchMethodException e) { Log.e(TAG, "getExternalFilesDir isn't available in this devices api"); } } </code></pre> <p>Now <a href="http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29">getExternalFilesDir()</a> is only available on API level 8 or above, so I want to use this if they have (Froyo), but otherwise I need another method.</p> <p>Now I have my test for the method I can go ahead and attempt to use it:</p> <pre><code> if(ClassThatExtendsApplication.getExternalFilesDir != null){ Object arglist[] = new Object[1]; arglist[0] = null; File path = (File) ClassThatExtendsApplication.getExternalFilesDir.invoke(context, arglist); // etc etc } else { // Not available do something else (like your deprecated methods / or load a different class / or notify they should get a newer version of Android to enhance your app ;-)) } </code></pre> <p>Hope that helps and shortcuts a lot of googling :-)</p> <p>P.S. if in the else you want to use your deprectated methods still, just add the <code>@SuppressWarnings("deprecation")</code> annotation above it, This will get rid of the warning and you have done it for the right reasons as you are using the latest API's when possible.</p>
 

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