我使用反射挂载/卸载外部storage.it的版本低于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
}
}任何获得它的解决方法都会抛出安全保护要求。我已经在清单中声明了这一点。我向谷歌了解了这个问题,我发现权限有working.As |signature Exception.android.permission.mount_unmount_filesystems level.Thanks。
发布于 2014-08-22 21:15:00
为了使用具有signature | system权限的东西,您的包必须由平台的签名密钥签名。除非您正在创建自己的自定义ROM或具有根设备,否则您将无法做到这一点。
如果你的应用程序是一个常规的第三方应用程序(在Play商店发布),那么你应该只使用公共API,而不是依赖于反射。只有公开的Android API被认为是稳定和公开的。另一些则是隐藏的,因为它们仅供系统内部使用。
https://stackoverflow.com/questions/24647143
复制相似问题