Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In the end I solved it by using a Proxy class. Here is the code!</p> <pre><code>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 onwards) // we use a Proxy class for implementing the listener public void setOnAudioFocusChangeListener() { Log.i(this, "setOnAudioFocusChangeListener()"); Class&lt;?&gt;[] innerClasses = theAudioManager.getClass().getDeclaredClasses(); for (Class&lt;?&gt; interfaze : innerClasses) { if (interfaze.getSimpleName().equalsIgnoreCase("OnAudioFocusChangeListener")) { Class&lt;?&gt;[] classArray = new Class&lt;?&gt;[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().getDeclaredMethods(); 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().getDeclaredMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("abandonAudioFocus")) method.invoke(theAudioManager, myOnAudioFocusChangeListener); } } catch (Exception e) { Log.e(this, e.getMessage()); } } } </code></pre> <p><strong>PROXY OnAudioFocusChangeListener class</strong></p> <pre><code>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[] args) throws Throwable { Object result = null; try { if (args != null) { if (method.getName().equals("onAudioFocusChange") &amp;&amp; args[0] instanceof Integer) { onAudioFocusChange((Integer) args[0]); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } } </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