register Media Button Event Receiver Compat - Android android.provider

Android examples for android.provider:MediaStore

Description

register Media Button Event Receiver Compat

Demo Code


//package com.java2s;
import android.content.ComponentName;
import android.media.AudioManager;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    private static final String TAG = "MediaButtonHelper";
    static Method sMethodRegisterMediaButtonEventReceiver;

    public static void registerMediaButtonEventReceiverCompat(
            AudioManager audioManager, ComponentName receiver) {
        if (sMethodRegisterMediaButtonEventReceiver == null)
            return;

        try {/*from w w w  .  ja va 2 s . co  m*/
            sMethodRegisterMediaButtonEventReceiver.invoke(audioManager,
                    receiver);
        } catch (InvocationTargetException e) {
            // Unpack original exception when possible
            Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else if (cause instanceof Error) {
                throw (Error) cause;
            } else {
                // Unexpected checked exception; wrap and re-throw
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException e) {
            Log.e(TAG,
                    "IllegalAccessException invoking registerMediaButtonEventReceiver.");
            e.printStackTrace();
        }
    }
}

Related Tutorials