最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】android中尝试使用Timer的schedule的TimerTask去实现循环的USB设备插入动作的检测

Android crifan 3915浏览 0评论

【背景】

折腾:

【已解决】Android设备作为Host希望实现可以检测到USB设备插入

期间,已经可以:

【记录】Android中创建进程或线程去实现USB设备插入的状态检测

了,但是:

后来看到:

Why does the thread run only once?

中的Timer的schedule的TimerTask,所以打算去试试

使用Timer的schedule的TimerTask去实现循环的USB设备插入动作的检测

【折腾过程】

1.去试试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private void usbActionDetectViaTimer(){
    //0x0403 / 0x6001: FTDI FT232R UART
    final int ft232rUartVid = 0x0403; //1027
    final int ft232rUartPid = 0x6001; //24577
 
    initUsbDetectHandler();
     
    final Timer detectTimer = new Timer();
    TimerTask detectTimerTask = new TimerTask() {
        public void run() {
            gLogger.debug("Begin check usb action");
 
            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            while(deviceIterator.hasNext()){
                UsbDevice device = deviceIterator.next();
                int usbVid = device.getVendorId();
                int usbPid = device.getProductId();
                if((usbVid == ft232rUartVid) && (usbPid ==ft232rUartPid) ){
                    //Toast.makeText(getApplicationContext(), "Found Usb device: FT232R UART", Toast.LENGTH_LONG).show();
                    //Toast.makeText(getApplicationContext(), "Now send message USB_ACTION_ATTACH to activity ", Toast.LENGTH_LONG).show();
                    gLogger.debug("Found Usb device: FT232R UART");
                    gLogger.debug("Now send message USB_ACTION_ATTACH to activit");
                     
                    Message foundUsbDeviceAttachMsg =new Message();
                    foundUsbDeviceAttachMsg.what=usb_action.USB_ACTION_ATTACH.getAction();
                    mHandler.sendMessage(foundUsbDeviceAttachMsg);
                     
                    detectTimer.cancel();
     
                    break;
                }
                else
                {
                    //Toast.makeText(getApplicationContext(), "Found USB VID="+usbVid+" PID=" + usbPid, Toast.LENGTH_LONG).show();
                    gLogger.debug("Found USB VID="+usbVid+" PID=" + usbPid);
                }
            }//while(deviceIterator.hasNext()){
    }};
     
    detectTimer.scheduleAtFixedRate(detectTimerTask, 0, detectIntervalInMs);
}

结果是,

好像是可以检测到的。

2.不过貌似

detectTimer.cancel();

后,还是会被执行到???

后来确定:

之前代码是正常的。

的确是:

detectTimer.cancel();

后续就没法继续执行了。

也就是:

上述代码,实际上,是可以正常工作的。

可以去动态的,每200ms去检测一次

如果检测到了,则发送message给handler

然后退出

如果没有检测到,继续检测。

 

【总结】

timer这个TimerTask,感觉应该是比Thread轻量级,基本够此处使用了。

转载请注明:在路上 » 【记录】android中尝试使用Timer的schedule的TimerTask去实现循环的USB设备插入动作的检测

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 求全部代码
    波波7年前 (2018-04-26)回复
86 queries in 0.191 seconds, using 22.18MB memory