headset button 的使用

android.permission.BLUETOOTH 权限

MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE);
registerReceiver(mMediaButtonReceiver, mediaFilter);

 

不要忘记设置优先权

 

public class HardButtonReceiver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

Log.v(“TestApp”, “Button press received”);

abortBroadcast();

KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

if(key.getAction() == KeyEvent.ACTION_UP)

{

int keycode = key.getKeyCode();

if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT)

{

Log.d(“TestApp”, “Next Pressed”);

}

else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)

{

Log.d(“TestApp”, “Previous pressed”);

}

else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK)

{

Log.d(“TestApp”, “Head Set Hook pressed”);

}

}

}

}

 

因为有些按键不一定被映射却要检查

你可能感兴趣的:(android,UP)