android 监听 USB 拔插广播消息
1、定义广播类:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class USBBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.hardware.usb.action.USB_STATE")){
if (intent.getExtras().getBoolean("connected")){
// usb 插入
Toast.makeText(context, "插入", Toast.LENGTH_LONG).show();
}else{
// usb 拔出
Toast.makeText(context, "拔出", Toast.LENGTH_LONG).show();
}
}
}
}2、在AndroidManifest.xml中注册广播:
<receiver android:name=".USBBroadcastReceiver">
<intent-filter android:priority="800">
<action android:name="android.hardware.usb.action.USB_STATE"/>
</intent-filter>
</receiver> 相关推荐
xilove0 2020-01-14
fuzhangandroid 2011-04-07
老菜鸟自习室 2011-08-05
mingming 2015-01-13
taiyuanwuyin 2015-03-31
xzw 2016-06-06
OliverLau 2019-06-29
chenjinlong 2020-04-06
magic00 2020-01-10
xilove0 2019-12-09
ThedakeLaugh 2010-08-23
StarkHuang 2014-09-04
易辰Android 2012-05-08