【问题标题】:Android: Detect When SD Card Mounted as Disk Drive to a ComputerAndroid:检测 SD 卡何时作为磁盘驱动器安装到计算机
【发布时间】:2012-04-22 17:54:58
【问题描述】:

我正在编写一个应用程序,该应用程序需要检测 SD 卡何时作为磁盘驱动器通过 USB 安装到计算机上,或者何时被手动移除。为此,我尝试使用广播接收器,但没有调用 onReceive。我的代码如下。

IntentFilter filter2 = new IntentFilter();
        //filter2.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        filter2.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        filter2.addAction(Intent.ACTION_MEDIA_SHARED);
        filter2.addAction(Intent.ACTION_MEDIA_REMOVED);
        filter2.addAction(Intent.ACTION_MEDIA_MOUNTED);

        registerReceiver(new CustomBroadcastReceiver(), filter2);

我的广播接收器如下...

public class CustomBroadcastReceiver extends BroadcastReceiver{

    public CustomBroadcastReceiver(){

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if(action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_SHARED) || action.equals(Intent.ACTION_MEDIA_REMOVED)){
            HardwareManager.IS_MEDIA_MOUNTED = false;
        }else if(action.equals(Intent.ACTION_MEDIA_MOUNTED)){
            HardwareManager.IS_MEDIA_MOUNTED = true;
        }else if(action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
            HardwareManager.IN_AIRPLANE_MODE = intent.getBooleanExtra("state", false);
        }
    }

}

当我通过 USB 作为磁盘驱动器连接时,onReceive 方法不会触发。

我做错了什么?

【问题讨论】:

    标签: android broadcastreceiver android-sdcard


    【解决方案1】:

    SDK 级别目前不支持此功能。

    来源:Android: Detect USB flash drive plugged in

    【讨论】:

    • Android NDK 可以做到这一点吗?
    • 我不知道。如果我有任何提示会通知你。
    • 我认为收听 ACTION_UMS_CONNECTED 将适用于 API 版本 8,而 android.os.storage.StorageEventListener 将适用于更高版本的 API。
    • 更正,ACTION_UMS_CONNECTED 也没有触发。我使用了stackoverflow.com/questions/4600896/android-detecting-usb 发布的最终答案,并使用该功能通过线程定期检查 SD 卡状态。
    • 鹤山,感谢您提供信息并选择我的答案。 :) 你能在 Android 2.2+ (2.3, 2.3.3, 3.x ...) 上测试Intent.ACTION_UMS_CONNECTED 吗?
    【解决方案2】:

    似乎 ACTION_UMS_CONNECTED 不起作用。我需要使用 API 版本 8,因此任何 android.os.storage.StorageEventListener 都不适用。

    我使用here 发布的答案来更新线程中的静态变量。这也可以用于执行自定义广播。方法如下... *注意:HardwareManager 是一个用户定义的类

    private static class SDCardMountStatusMonitor implements Runnable{
    
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                try {
                    HardwareManager.IS_MEDIA_MOUNTED = sdIsCardMounted(); //YOU CAN BROADCAST SOMETHING FROM HERE INSTEAD
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }
    
    public static boolean sdIsCardMounted() {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                return true;
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                return false;
            } else {
                return false;
            }
    }
    

    然后在另一个地方,我从这个 Runnable 创建一个线程并启动它。

    Thread t = new Thread(new SDCardMountStatusMonitor());
    t.start();
    

    【讨论】:

      【解决方案3】:

      您必须添加“文件”方案 (addDataScheme("file")) 以使 IntentFilter 匹配广播 ACTION_MEDIA_* 操作意图。

      ACTION_MEDIA_* 意图在 Intent.mData 字段中具有到挂载点的路径(请参阅Intent docs),但没有设置方案的 IntentFilter 只会匹配不包含数据的意图(请参阅IntentFilter docs)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-10
        • 2016-07-15
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 2015-04-05
        • 2023-03-26
        • 2011-12-31
        相关资源
        最近更新 更多