2011-09-21 35 views
8

Devo sviluppare un'applicazione per Android 1.6 (API 4), che dovrebbe essere in grado di utilizzare OnAudioFocusChangeListener (disponibile da Android 2.2 - API 8) nei telefoni con Android 2.2 o dopo.come istanziare un ascoltatore per riflessione in Android

Chiunque può dirmi come istanziare un ascoltatore per riflessione? Sono già riuscito a eseguire metodi statici e anche non statici per riflessione, ma non so come fare con gli ascoltatori.

Questa è l'ascoltatore a riflettere:

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); 
} 

Questo è un esempio di codice con i metodi sono riuscito a correre per riflessione:

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

Ecco buon esempio http://blogs.oracle.com/poonam/entry/how_to_implement_an_interface – Ronnie

risposta

6

Alla fine l'ho risolto utilizzando una classe Proxy. Ecco il codice!

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()); 
     } 
    } 
} 

DELEGA OnAudioFocusChangeListener classe

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; 
    } 
} 
1

IMHO riflessione renderà le vostre classi meno leggibile. Anche la riflessione è un po 'più lenta del normale accesso al campo o alla classe.

In alternativa vedere l'approccio classe wrapper descritto qui: http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html

Creare interfaccia e due implementazioni di esso, uno per API 8+ e l'altro per le versioni precedenti. Nella tua classe API8 puoi utilizzare le classi API 8 incluso OnAudioFocusChangeListener. Quindi creare un'istanza della versione in base alla versione del sistema operativo, che è possibile verificare tramite Build.VERSION.SDK_INT.

+0

io non sono del tutto sicuro che avrei potuto utilizzare un wrapper, come Ho bisogno di passare un oggetto 'OnAudioFocusChangeListener' come argomento ai metodi' requestAudioFocus' e 'abandonAudioFocus' dell'oggetto' AudioManager'. Suppongo che non funzioni se si passa un wrapper OnAudioFocusChangeListener. –

+0

Puoi averlo come una classe interna all'interno della classe wrapper. Questo funzionerebbe. –

+0

Vorrei anche perdere il riflesso - è odore di codice - specialmente quando c'è un modo migliore per risolverlo. Usa il metodo wrapper - molto più bello. – Martyn

Problemi correlati