mAudioManager.registerMediaButtonEventReceiver(new ComponentName(
this,
MusicIntentReceiver.class));
@SuppressLint("InlinedApi")
public class MusicIntentReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "jni";
private Context mContext;
private KeyService mKeyService;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mKeyService = new KeyService(mContext);
if (intent.getAction().equals(
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
Toast.makeText(context, "Headphones disconnected.",
Toast.LENGTH_SHORT).show();
Log.i(LOG_TAG, "Headphones disconnected.!");
// send an intent to our MusicService to telling it to pause the
// audio
// context.startService(new Intent(MusicService.ACTION_PAUSE));
} else if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
Log.i(LOG_TAG, "ACTION_MEDIA_BUTTON!");
KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(
Intent.EXTRA_KEY_EVENT);
if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
return;
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
// context.startService(new
// Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
// context.startService(new Intent(MusicService.ACTION_PLAY));
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
// context.startService(new Intent(MusicService.ACTION_PAUSE));
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
// context.startService(new Intent(MusicService.ACTION_STOP));
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
// context.startService(new Intent(MusicService.ACTION_SKIP));
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
// TODO: ensure that doing this in rapid succession actually
// plays the
// previous song
// context.startService(new Intent(MusicService.ACTION_REWIND));
break;
}
} else if (intent.getAction().equals(
BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
int state = intent.getIntExtra(
BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
if (state == BluetoothAdapter.STATE_CONNECTED
) {
Log.i(LOG_TAG, "BluetoothAdapter.ACTION_CONNECTION_STATE_CONNECT");
}
else if (state == BluetoothAdapter.STATE_DISCONNECTED){
Log.i(LOG_TAG, "BluetoothAdapter.ACTION_CONNECTION_STATE_DISCONNECTED");
}
} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(LOG_TAG, "Intent.ACTION_BOOT_COMPLETED");
} else if (intent.getAction().equals("android.intent.action.UPDATE_SUSPEND_TIME_BY_HAND")) {
Log.i(LOG_TAG, "Intent.UPDATE_SUSPEND_TIME_BY_HAND");
} else {
// Log.i(LOG_TAG, "other intent");
}
}
4. 开启线程监控 控制录音