android 监听USB连接状态(加声音提示)
1.private final static String ACTION =
"android.hardware.usb.action.USB_STATE";
private SoundPool soundPool;
private int times=0;
2.IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(usBroadcastReceiver, filter);
soundPool= new
SoundPool(10,AudioManager.STREAM_SYSTEM,5);
soundPool.load(this,R.raw.aejat,1);
3.
BroadcastReceiver usBroadcastReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(ACTION)) {
boolean connected =
intent.getExtras().getBoolean("connected");
if(times==0&&connected)
{
soundPool.play(1, 1, 1, 0, 0, 1);
times=1;
}else if((times == 1) && !connected){
soundPool.play(1, 1, 1, 0, 0, 1);
times = 0;
}else{
return; // // 插入后面的多次调用,直接返回。
}
}
}
};
4.
android:minSdkVersion="12"
日拱一卒百无有尽,功不唐捐终入海
浙公网安备 33010602011771号