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

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

问题描述

我创建了一个webview应用程序,该应用程序在资产目录中的应用程序中托管一个网站. 我想通过插入平板电脑usbslot中的USB棒更新网站. 我首先尝试了.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检查始终运行是另外一个问题.我认为我必须将其放入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()获取到mount根目录的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天全站免登陆