Android 检测 kitkat 的 USB 存储 (4.4) [英] Android detect usb storage for kitkat (4.4)

查看:35
本文介绍了Android 检测 kitkat 的 USB 存储 (4.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 webview 应用程序,它在资产目录中的应用程序内托管一个网站.我想通过插入我的平板电脑 usbslot 的 U 盘更新网站.我首先尝试了 .MEDIA_MOUNTED 广播,它不适用于我的 android 4.4.药片.我已经搜索了替代方法并找到了MediaScannerConnection".这里有几个例子,对解决我的问题没有多大帮助.

I've created a webview app which hosts a website within the app in the assets-directory. I want to update the website via an USBstick inserted in my tablets usbslot. I tried it first with the .MEDIA_MOUNTED broadcast which doesn't work for my android 4.4. Tablet. I've searched for an alternative and found the "MediaScannerConnection". There are several examples here, which didn't help me much, to solve my issue.

我正在寻找一个简单而干净的解决方案,稍微解释一下也很好,以检测是否连接了 USB 存储以及之后执行一些代码的机会.另外如何让这个USB-check一直运行是一个问题.我想我必须把它放在 OnResume 方法中,但我不太确定.

I'm looking for an easy and clean solution and a little explanation would be nice too, to detect if an USB-Storage is connected and the chance to execute some code afterwards. And how make this USB-check run all the time is a question in addition. I assume i have to put it in the OnResume method, but i'm not quite shure.

推荐答案

请发布您的 BroadcastReceiver 和 AndroidManifest.xml 的代码.

Please post code for your BroadcastReceiver and AndroidManifest.xml.

清单应该看起来像

<application>
    <!--- ... --->
    <receiver android:name=".UsbBroadcastReceiver"
            android:exported="true"
            android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>
</application>

注意 data 标签,它经常被忽视.

Note the data tag, it's often overlooked.

在您的 onReceive 实现中,可以从 Intent#getData() 获取到挂载根目录的 URI.

In your onReceive implementation the URI to root of mount can be obtained from Intent#getData().

另请注意,这不适用于 Android 6.0 (M) 及更高版本.对于 6.0 及更高版本,您的代码必须通过以下两种方式之一请求访问 USB:

Also note that this will not work on Android 6.0 (M) and above. For 6.0 and above your code must request access to USB via one of two ways:

1) UsbManager#requestPermission

2) Intent#ACTION_OPEN_DOCUMENT_TREE 作为存储访问框架

这篇关于Android 检测 kitkat 的 USB 存储 (4.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆