为什么我的监听USB插拔广播接收器只能监听一次
答案:1 悬赏:0
解决时间 2021-03-28 05:21
- 提问者网友:锁深秋
- 2021-03-28 02:00
为什么我的监听USB插拔广播接收器只能监听一次
最佳答案
- 二级知识专家网友:执傲
- 2021-03-28 02:25
用这段代码在你的广播接收器添加就会监听到插进拔出
if (intent.getAction().equalsIgnoreCase(
"android.hardware.usb.action.USB_STATE")) {
if (intent.getExtras().getBoolean("connected")) {
Toast.makeText(context, "USB已连接", Toast.LENGTH_SHORT).show();
Intent intent1=new Intent(context, UsbConMyService.class);
context.startService(intent1);
} else {
Toast.makeText(context, "USB已断开", Toast.LENGTH_SHORT).show();
Intent intent2=new Intent(context, UsbDisconMyService.class);
context.startService(intent2);
}
if (intent.getAction().equalsIgnoreCase(
"android.hardware.usb.action.USB_STATE")) {
if (intent.getExtras().getBoolean("connected")) {
Toast.makeText(context, "USB已连接", Toast.LENGTH_SHORT).show();
Intent intent1=new Intent(context, UsbConMyService.class);
context.startService(intent1);
} else {
Toast.makeText(context, "USB已断开", Toast.LENGTH_SHORT).show();
Intent intent2=new Intent(context, UsbDisconMyService.class);
context.startService(intent2);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯