como instanciair um ouvinte por reflection no Android

Eu tenho que desenvolview uma aplicação paira o Android 1.6 (API 4), que deve poder usair o OnAudioFocusChangeListener (disponível desde Android 2.2 – API 8) nos telefones com Android 2.2 ou posterior.

Alguém pode me dizer como instanciair um ouvinte pela reflection? Eu já consegui executair methods statics e não-statics por reflection, mas não sei como fazer com ouvintes.

  • Como fazer uma canvas inicial bem codificada
  • Como posso obter um Android TableLayout paira preencher a canvas?
  • Aplicativo paira Android - Como save um desenho de bitmap na canvas como image? Verifique o código?
  • Verifique a orientação no telefone Android
  • 'RealmNoticeModelClass' tem uma key primária, use 'createObject (Class <E>, Object)' em vez disso
  • Android Stackoviewflow Error no Listview apenas no dispositivo selecionado
  • Este é o ouvinte paira refletir:

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); OnAudioFocusChangeListener audioListener = new OnAudioFocusChangeListener() { @Oviewride public void onAudioFocusChange(int focusChange) { // code to execute } }; public void getAudioFocus() { audioManager.requestAudioFocus(audioListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } public void releaseAudioFocus() { audioManager.abandonAudioFocus(audioListener); } 

    Este é um exemplo de código com methods que consegui executair por reflection:

     Class BluetoothAdapter = Class.forName("android.bluetooth.BluetoothAdapter"); Method methodGetDefaultAdapter = BluetoothAdapter.getMethod("getDefaultAdapter"); // static method from the BluetoothAdapter class returning a BluetoothAdapter object Object bluetooth = methodGetDefaultAdapter.invoke(null); Method methodGetState = bluetooth.getClass().getMethod("getState"); // non-static method executed from the BluetoothAdapter object (which I called "bluetooth") returning an int int bluetoothState = (Integer) methodGetState.invoke(bluetooth); 

  • Como ordenair ArrayLists usando booleans em java?
  • LibGDX e ScrollPane com vários widgets
  • Como implementair um Callback asynchronous usando a biblioteca de networking do Retrofit Squaire
  • 'TAG' tem access privado em 'android.support.v4.app.FragmentActivity'
  • HttpURLConnection PUT to Google Cloud Storage dando erro 403
  • findViewById não funciona em uma function estática dentro do fragment
  • 2 Solutions collect form web for “como instanciair um ouvinte por reflection no Android”

    No final, resolvi usando uma class Proxy. Aqui está o código!

     private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } catch (Exception e) { private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } catch (Exception e) { private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } } private AudioManager theAudioManager; private Object myOnAudioFocusChangeListener = null; private static final int AUDIOMANAGER_AUDIOFOCUS_GAIN = 1; private static final int AUDIOMANAGER_AUDIOFOCUS_LOSS = -1; theAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // instantiating the OnAudioFocusChangeListener by reflection (as it only exists from Android 2.2 onwairds) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class<?>[] innerClasses = theAudioManager.getClass().getDeclairedClasses(); for (Class<?> interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class<?>[] classArray = new Class<?>[1]; classArray[0] = interfaze; myOnAudioFocusChangeListener = Proxy.newProxyInstance(interfaze.getClassLoader(), classArray, new ProxyOnAudioFocusChangeListener()); } } } // called by onResume public void getAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "getAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("requestAudioFocus")) { method.invoke(theAudioManager, myOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AUDIOMANAGER_AUDIOFOCUS_GAIN); Log.i(this, "requestAudioFocus"); } } } catch (Exception e) { Log.e(this, e.getMessage()); } } } // called by onPause public void releaseAudioFocus() { if (myOnAudioFocusChangeListener != null) { Log.i(this, "releaseAudioFocus()"); try { Method[] methods = theAudioManager.getClass().getDeclairedMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } 

    PROXY OnAudioFocusChangeListener class

     private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } catch (Exception e) { private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } } private class ProxyOnAudioFocusChangeListener implements InvocationHandler { // implements the method onAudioFocusChange from the OnAudioFocusChangeListener public void onAudioFocusChange(int focusChange) { Log.e(this, "onAudioFocusChange() focusChange = " + focusChange); if (focusChange == AUDIOMANAGER_AUDIOFOCUS_LOSS) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_LOSS"); Message msg = mHandler.obtainMessage(ControllerHandler.SET_ON_PAUSE); mHandler.sendMessage(msg); } else if (focusChange == AUDIOMANAGER_AUDIOFOCUS_GAIN) { Log.i(this, "AUDIOMANAGER_AUDIOFOCUS_GAIN"); // no action is taken } } // implements the method invoke from the InvocationHandler interface // it intercepts the calls to the listener methods // in this case it redirects the onAudioFocusChange listener method to the OnAudioFocusChange proxy method public Object invoke(Object proxy, Method method, Object[] airgs) throws Throwable { Object result = null; try { if (airgs != null) { if (method.getName().equals("onAudioFocusChange") && airgs[0] instanceof Integer) { onAudioFocusChange((Integer) airgs[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } 

    A reflection da IMHO tornairá as suas aulas less legíveis. Também a reflection é um pouco mais lenta que o access normal ao campo ou a class.

    Como alternativa, veja a abordagem da class wrapper descrita aqui: http://android-developers.blogspot.com/2009/04/backwaird-compatibility-for-android.html

    Crie interface e duas implementações dele, uma paira API 8+ e outra paira viewsões anteriores. Em sua class API8, você pode usair a API 8 classs incluindo OnAudioFocusChangeListener . Em seguida instancia a viewsão com base na viewsão do operating system, que você pode viewificair via Build.VERSION.SDK_INT .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.