Imountservice to mount/unmount volume not working on 4.4 Android

孤街浪徒 提交于 2019-12-25 01:42:39

问题


I have used reflection to mount/unmount external storage.it is working below 4.4 Api. code is below

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
    }
}

Any workaround to get it working.As it throws Security Exception.android.permission.mount_unmount_filesystems requires.I have decleared this in manifest.I have google about this issue i found that the permission have system|signature protection level.Thanks in advance.


回答1:


In order to use something with signature | system permissions your package has to be signed by the platform's signing key. Unless you're creating your own custom ROM or have a rooted device, you won't be able to do this.

If your app is a regular 3rd party app (released in the Play store) then you should only use the public APIs and not depend on reflection. Only the public Android APIs are considered stable and exposed. Others are hidden because they are only intended to be used by the internals of the system.



来源:https://stackoverflow.com/questions/24647143/imountservice-to-mount-unmount-volume-not-working-on-4-4-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!