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"
posted @ 2016-07-21 15:07  稻香鱼  阅读(1274)  评论(0)    收藏  举报