Android: Detecting USB

前端 未结 6 1865
一生所求
一生所求 2020-11-27 06:13

Is there any way to know(programmatically) in your Activity/Application that the user has connected your phone to PC through USB?

6条回答
  •  天命终不由人
    2020-11-27 06:26

    If all you want to do is detect whether you have access to the sdcard, then the following will work:

    private boolean canWriteToFlash() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // Read only isn't good enough
            return false;
        } else {
            return false;
        }
    }
    

提交回复
热议问题