Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6621093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:11:41+00:00 2026-05-25T21:11:41+00:00

I have to develop an application for Android 1.6 (API 4), which should be

  • 0

I have to develop an application for Android 1.6 (API 4), which should be able to use the OnAudioFocusChangeListener (available since Android 2.2 – API 8) in the phones with Android 2.2 or later.

Anyone can tell me how to instantiate a listener by reflection?
I have already managed to run static and also non-static methods by reflection, but I don’t know how to do with listeners.

This is the listener to reflect:

AudioManager  audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

OnAudioFocusChangeListener audioListener = new OnAudioFocusChangeListener() {
    @Override
    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);
}

This is a code example with methods I managed to run by 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:11:41+00:00Added an answer on May 25, 2026 at 9:11 pm

    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 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().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") && args[0] instanceof Integer) {
                        onAudioFocusChange((Integer) args[0]);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
            }
            return result;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to develop an Android application which should access a specific version of
I need to develop an application on both iphone and android which have to
Hi i have develop an android application in which i want to prevent user
I have a requirement to develop a shopping cart Android application. The features should
I have to develop an application which parses a log file and sends specific
I have to develop an application with which my client will do visual design.
I occur a problem when develop a application on android. There have two image
I have just started with android ... I have to develop an android application
I want to develop an application in iPhone and Android both which can display
i have develop my application in android. Now i want to make Home Screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.