How to instantiate a listener by reflection in Android?

IMHO reflection will make your classes less readable. Also reflection is quite a bit slower then normal field or class access As an alternative see the wrapper class approach described here: android-developers.blogspot.com/2009/04/... Create interface and two implementations of it, one for API 8+ and the other for the earlier versions. In your API8 class you can use API 8 classes including OnAudioFocusChangeListener Then instantiate the version based on version of OS, which you can check via Build.VERSION.

SDK_INT.

IMHO reflection will make your classes less readable. Also reflection is quite a bit slower then normal field or class access. As an alternative see the wrapper class approach described here: android-developers.blogspot.com/2009/04/... Create interface and two implementations of it, one for API 8+ and the other for the earlier versions.In your API8 class you can use API 8 classes including OnAudioFocusChangeListener.

Then instantiate the version based on version of OS, which you can check via Build.VERSION. SDK_INT.

I am not completely sure I could use a wrapper, as I need to pass an OnAudioFocusChangeListener object as an argument to the requestAudioFocus and abandonAudioFocus methods of the AudioManager object. I suppose it doesn't work if a pass OnAudioFocusChangeListener wrapper instead. – Daniele B Sep 22 at 14:42 You can have it as an inner class inside wrapper class.

This would work. – Peter Knego Sep 22 at 15:46 I would also give the reflection a miss - it's code smell - especially when there's a better way to solve it. Use the wrapper method - much nicer.

– Martyn Sep 28 at 20:30 The wrapper method has the disadvantage of requiring the target sdk version to be set to the higher one. This means that you need to be careful that all classes available in the higher APIs don't get executed by devices with lower APIs. As eclipse (as far as I know) is not able to highlight which code is available only on higher APIs and needs to be "blocked" to lower APIs, it's easy to forget to recode for the lower APIs.So, my suggestion it's to use reflection in case only one or two classes available in the higher APIs need to be used.

– Daniele B Sep 29 at 9:43 True, good point. But you can still make wrappers that include all version dependent code, one wrapper per version (or group of versions), then by simply switching target version in Eclipse, you see errors popping out in one place. This should make it easier to catch version-related errors.

– Peter Knego Sep 29 at 10:41.

In the end I solved it by using a Proxy class. Here is the 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 innerClasses = theAudioManager.getClass().

GetDeclaredClasses(); for (Class interfaze : innerClasses) { if (interfaze.getSimpleName(). EqualsIgnoreCase("OnAudioFocusChangeListener")) { Class classArray = new Class1; classArray0 = 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()); } } } // 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 args) throws Throwable { Object result = null; try { if (args! = null) { if (method.getName().

Equals("onAudioFocusChange") && args0 instanceof Integer) { onAudioFocusChange((Integer) args0); } } } catch (Exception e) { throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); } return result; } }.

Java - how to instantiate a listener by reflection in Android - Stack Overflow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions