send Media Button Event Intent - Android android.media

Android examples for android.media:Media

Description

send Media Button Event Intent

Demo Code

import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;

public class Main {

  /**//from w ww  . j a v a  2 s .  co m
   * Sends a media button event. 
   * @param keycode
   * @param context
   */
  public static void sendMediaButtonEvent(int keycode, Context context) {

    try {
      Process process = Runtime.getRuntime().exec("input keyevent " + keycode);
    } catch (IOException e) {
      e.printStackTrace();
    }

    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keycode));
    context.sendOrderedBroadcast(intent, null);

    // intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, keycode));
    context.sendOrderedBroadcast(intent, null);
  }

}

Related Tutorials