java - android 中的 USB 设置未存储

标签 java android android-layout android-intent android-framework

我是 android 框架的新手。我正在使用 USB 设置。我想知道 android 中存储的 USB 设置在哪里。 我有以下问题: 当我将手机连接到计算机并从大容量存储、MTP、PTP 中选择一个选项时,现在如果我关闭然后打开设备,我的选择将被清除。我希望保留之前的选择。 我该怎么办..??

提前致谢...

编辑:我今天发现应该在/data/system 文件夹中创建 usb_device_manager.xml 文件,但在我的例子中它没有生成。 我无法找到这背后的原因。 创建此文件的文件位于 frameworks/base/services/java/com/android/server/usb/

等待肯定的答复...

最佳答案

根据 http://developer.android.com/reference/android/hardware/usb/UsbManager.html 您找不到用于获取此信息的开放 API。 但是,从源码中我们可以看到这两个函数:

/**
 * Name of the MTP USB function.
 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
 *
 * {@hide}
 */
public static final String USB_FUNCTION_MTP = "mtp";

/**
 * Name of the PTP USB function.
 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
 *
 * {@hide}
 */
public static final String USB_FUNCTION_PTP = "ptp";


 /**
     * Returns the current default USB function.
     *
     * @return name of the default function.
     *
     * {@hide}
     */
    public String getDefaultFunction() {
        String functions = SystemProperties.get("persist.sys.usb.config", "");
        int commaIndex = functions.indexOf(',');
        if (commaIndex > 0) {
            return functions.substring(0, commaIndex);
        } else {
            return functions;
        }
    }

    /**
     * Sets the current USB function.
     * If function is null, then the current function is set to the default function.
     *
     * @param function name of the USB function, or null to restore the default function
     * @param makeDefault true if the function should be set as the new default function
     *
     * {@hide}
     */
    public void setCurrentFunction(String function, boolean makeDefault) {
        try {
            mService.setCurrentFunction(function, makeDefault);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setCurrentFunction", e);
        }
    }

不建议使用隐藏的 API,但这可能会解决您的问题。 如果您想知道如何使用隐藏的 API,请引用此链接: http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/

关于java - android 中的 USB 设置未存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894007/

相关文章:

java - 如何从 Spring Integration 中的方法返回值而不中断原始消息流?

android - Xamarin 是摄像头监控应用程序的完美工具吗?

Android 深度链接无法启动应用程序

android - 包含标记在运行时在 Xamarin .axml 布局中给出异常

android - ViewPager 终止后 fragment 布局不显示

android - 数据绑定(bind)布局奇怪的构建错误

java - 管理有关 .lnk 文件的元数据

java - 通过String方法删除或替换 "|"字符

java - 长时间运行的 Tomcat 进程的类加载器中的问题

Android位置更新问题 华为