android 连接usb包括充电

public class StatusUsbActivity extends Activity {
private SoundPool soundPool;
private int times=0;
private final static String ACTION = "android.intent.action.BATTERY_CHANGED";
private boolean connected=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

IntentFilter mIntentFilter = new IntentFilter(); 
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 
registerReceiver(BtStatusReceiver, mIntentFilter);

soundPool= new SoundPool(10,AudioManager.STREAM_SYSTEM,5);
soundPool.load(this,R.raw.aejat,1);
}

public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver() 
{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION)) {
int num = intent.getIntExtra("plugged", 0);
if (num==1||num==2) {
connected=true;
} else if (num == 0) {
connected=false;
}
if(times==0&&connected)
{
soundPool.play(1, 1, 1, 0, 0, 2);
times=1;
}else if((times == 1) && !connected){
soundPool.play(1, 1, 1, 0, 0, 2);
times = 0; 
}else{
return; // // 插入后面的多次调用,直接返回。
}

}
}
};
posted @ 2016-07-22 13:58  稻香鱼  阅读(209)  评论(0)    收藏  举报