[android-developers] Re: Writing Version-Specific Code
Here is an example for a custom audio manager that supports ducking (or not) package com.pocketgpsworld.cameralert; import android.content.Context; import android.media.AudioManager; import android.media.AudioManager.OnAudioFocusChangeListener; import android.os.Build; public abstract class CustomAudioManager { public abstract int requestAudioFocus(); public abstract int abandonAudioFocus(); public static CustomAudioManager newInstance(Context context) { final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); CustomAudioManager cam = null; if (sdkVersion 8) return cam; cam = new FroyoAudioManager(context.getApplicationContext()); return cam; } private static class FroyoAudioManager extends CustomAudioManager { AudioManager am; OnAudioFocusChangeListener l; public FroyoAudioManager(Context context) { am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); l = new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int arg0) { } }; } @Override public int abandonAudioFocus() { return am.abandonAudioFocus(l); } @Override public int requestAudioFocus() { return am.requestAudioFocus(l, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } } } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Writing Version-Specific Code
Here's an example of a custom audiomanager that supports ducking, but only for the newer OS versions package xyz; import android.content.Context; import android.media.AudioManager; import android.media.AudioManager.OnAudioFocusChangeListener; import android.os.Build; public abstract class CustomAudioManager { public abstract int requestAudioFocus(); public abstract int abandonAudioFocus(); public static CustomAudioManager newInstance(Context context) { final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); CustomAudioManager cam = null; if (sdkVersion 8) return cam; cam = new FroyoAudioManager(context.getApplicationContext()); return cam; } private static class FroyoAudioManager extends CustomAudioManager { AudioManager am; OnAudioFocusChangeListener l; public FroyoAudioManager(Context context) { am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); l = new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int arg0) { } }; } @Override public int abandonAudioFocus() { return am.abandonAudioFocus(l); } @Override public int requestAudioFocus() { return am.requestAudioFocus(l, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } } } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Writing Version-Specific Code
On Tue, Apr 12, 2011 at 11:12 PM, Jake Colman col...@ppllc.com wrote: Can you provide an example? https://github.com/commonsguy/cw-android/tree/master/Menus/ActionBarBC https://github.com/commonsguy/cw-android/tree/master/Prefs/FragmentsBC https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Picture to name three. I am especially looking to understand how I can use this technique to have, for example, a preferences screen that enables/disable an option (probably with an appropriate summary text) based on OS version. You don't need anything more than checking Build.VERSION.SDK_INT in an if statement for that, I would think. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9.2 Available! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Writing Version-Specific Code
http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html On Apr 12, 4:01 pm, Jake Colman col...@ppllc.com wrote: My application is targeted for v1.6 and greater. There is, however, one feature that I'd like to implement that would work only on v2.2. How does one write and publish code that will work on v1.6 yet make use of v2.2 APIs if they are available? How does one properly set up the Manifest to indicate that the app runs on v1.6 but would prefer v2.2? I read up on MinSDK but I am not clear on how it works for this purpose. An RTFM answer is fine if appropriate. Thanks. -- Jake Colman -- Android Tinkerer -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Writing Version-Specific Code
Here's a good writeup on how to do this. http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html The benefit of doing it this way vs leaving the min/target SDK at the lowest level and using reflection is that by setting the target SDK to the higher level you can include XML elements/attributes, in the resource files, that are introduced in the later API levels. Using reflection does not allow this, AFAIK. Note that you just have to make sure to not use those newer resources when running against an older API level, but that's really the same as knowing to not use the newer API calls when running against older API levels. Don On Apr 12, 4:01 pm, Jake Colman col...@ppllc.com wrote: My application is targeted for v1.6 and greater. There is, however, one feature that I'd like to implement that would work only on v2.2. How does one write and publish code that will work on v1.6 yet make use of v2.2 APIs if they are available? How does one properly set up the Manifest to indicate that the app runs on v1.6 but would prefer v2.2? I read up on MinSDK but I am not clear on how it works for this purpose. An RTFM answer is fine if appropriate. Thanks. -- Jake Colman -- Android Tinkerer -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Writing Version-Specific Code
MM == Mark Murphy mmur...@commonsware.com writes: MM On Tue, Apr 12, 2011 at 5:08 PM, lbendlin l...@bendlin.us wrote: It was also factually wrong, according to the all-knowing source of all wisdom (aka Wipikedia) Basically, use reflection to hide the new function from the compiler-complainer. MM I tend to use conditional class loading more, isolating MM version-specific code in classes that I can conditionally MM reference when it is safe to do so. Can you provide an example? I am especially looking to understand how I can use this technique to have, for example, a preferences screen that enables/disable an option (probably with an appropriate summary text) based on OS version. -- Jake Colman -- Android Tinkerer -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en