问题
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