【问题标题】:Android: How to Detect "Turn on USB storage" Broadcast?Android:如何检测“打开 USB 存储”广播?
【发布时间】:2012-01-09 16:00:00
【问题描述】:

我正在尝试使用 BroadcastReceiver 检测 打开 USB 存储,尽管我能够检测到使用 android.intent.action.UMS_CONNECTED 操作连接的 USB

使用 android.intent.action.UMS_DISCONNECTED 操作断开连接。


如何检测 USB 存储

【问题讨论】:

    标签: java android broadcastreceiver


    【解决方案1】:

    以下是我如何检查存储卡是否已安装/卸载。您可以更改它以检查已删除/已插入。我通过注册一个 BroadcastReceiver 来获取“挂载事件”然后检查存储卡处于什么状态。如果它没有被挂载并且在它检查时不是(在它再次挂载卡期间的状态)它被卸载或该卡已被移除。

    public class MemCardReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                onMemcardMounted();
            }
            else if (!Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)){
                onMemorycardUnMounted();
            }
        }
    
        private void onMemorycardUnMounted() {}
    
        private void onMemcardMounted() {}
    }
    

    在清单文件中

    <receiver android:enabled="true" android:exported="true" android:name="the.name"> 
            <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>
    

    如果有任何其他类似的声明,checkout this 有几种不同的状态。删除

    【讨论】:

      【解决方案2】:

      我认为 android.Intent.action.ACTION_MEDIA_EJECT 是在用户使用正在使用的 USB 存储设备时广播,而在它关闭时是 ACTION_MEDIA_MOUNTED。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2014-07-17
        • 2022-12-13
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多