在安装4.4 Android时无法安装/卸载卷的Imountservice

时间:2014-07-09 06:55:10

标签: android sd-card

我使用反射来挂载/卸载外部存储。它的工作速度低于4.4 Api。 代码在下面

import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;            
import android.os.storage.IMountService; 


private static final String MOUNT_POINT = "/mnt/ext_usb" or "/mnt/sdcard/" ...
private IMountService mMountService = null;

private synchronized IMountService getMountService() {
    if (mMountService == null) {
        IBinder service = ServiceManager.getService("mount");
        if (service != null) {
            mMountService = IMountService.Stub.asInterface(service);
        } else {
            Log.e(TAG, "Can't get mount service");
        }
    }
    return mMountService;
}

private void mount() {
    IMountService mountService = getMountService();
    try {
        if (mountService != null) {
            mountService.mountVolume(MOUNT_POINT);
        } else {
            //
        }
    } catch (RemoteException ex) {
        // Not much can be done
    }
}

private void unmount() {
    StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
    String state = sm.getVolumeState(MOUNT_POINT);
    if (!Environment.MEDIA_MOUNTED.equals(state) &&
            !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        //
        return;
    }

    IMountService mountService = getMountService();
    try {
        if (mountService != null) {
            mountService.unmountVolume(MOUNT_POINT, true, false);
        } else {
            Log.e(TAG, "Mount service is null, can't unmount");
        }
    } catch (RemoteException ex) {
        // Not much can be done
    }
}

任何解决方法让它工作。因为它抛出Security Exception.android.permission.mount_unmount_filesystems require.I已在清单中删除了这个。我有谷歌关于这个问题我发现该权限具有系统|签名保护级别。谢谢提前。

1 个答案:

答案 0 :(得分:1)

为了使用具有signature | system权限的内容,您的包必须由平台的签名密钥签名。除非您正在创建自己的自定义ROM或具有root设备,否则您将无法执行此操作。

如果您的应用是普通的第三方应用(在Play商店中发布),那么您应该只使用公共API而不依赖于反射。只有公共Android API被认为是稳定且公开的。其他的是隐藏的,因为它们仅供系统内部使用。