提交 b789dc94 编写于 作者: 只猪会上树's avatar 只猪会上树

上传新文件

上级 1249e955
package example.com.save;
/**
* 耳机连接状态广播接收器
*/
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class HeadsetReceiver extends BroadcastReceiver {
private OnHeadsetListener onHeadsetListener;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//蓝牙耳机
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
int state = adapter.getProfileConnectionState(BluetoothProfile.HEADSET);
if (BluetoothProfile.STATE_CONNECTED == state) {
onHeadsetListener.isHeadsetConnected(true);
}
if (BluetoothProfile.STATE_DISCONNECTING == state) {
onHeadsetListener.isHeadsetConnected(false);
}
}
//有线耳机
else if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
if (intent.hasExtra("state")) {
int state = intent.getIntExtra("state", 0);
if (state == 1) {
//插入耳机
onHeadsetListener.isHeadsetConnected(true);
} else if (state == 0) {
//拔出耳机
onHeadsetListener.isHeadsetConnected(false);
}
}
}
}
public void setOnHeadsetListener(OnHeadsetListener onHeadsetListener) {
this.onHeadsetListener = onHeadsetListener;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册