next Track when key down event - Android android.os

Android examples for android.os:IBinder

Description

next Track when key down event

Demo Code

import android.content.Context;
import android.os.IBinder;
import android.view.KeyEvent;

public class Main {

  public static void nextTrack() {
    dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
  }/*ww w. j av  a2  s .  c o  m*/

  public static void dispatchMediaKeyEvent(KeyEvent keyEvent) {
   try {

      IBinder iBinder = (IBinder) Class.forName("android.os.ServiceManager")
          .getDeclaredMethod("checkService", String.class).invoke(null, Context.AUDIO_SERVICE);

      Object audioService = Class.forName("android.media.IAudioService$Stub")
          .getDeclaredMethod("asInterface", IBinder.class).invoke(null, iBinder);

      Class.forName("android.media.IAudioService").getDeclaredMethod("dispatchMediaKeyEvent", KeyEvent.class)
          .invoke(audioService, keyEvent);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Related Tutorials