80,472
社区成员




@Override
public void run() {
if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission("android.permission.MOUNT_UNMOUNT_FILESYSTEMS")) {
String[] vols = getVolumePaths();
if (vols != null) {
for (String volume : vols) {
if (volume.contains("sdcard")) {
unmount(volume);
}
}
}
} else {
GdLog.i("permission.MOUNT_UNMOUNT_FLESYSTEMS not granted");
}
}
private String[] getVolumePaths() {
StorageManager storageMgr = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
try {
Method getVolPaths = storageMgr.getClass().getMethod("getVolumePaths");
return (String[])getVolPaths.invoke(storageMgr);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
private boolean unmount(String volume) {
try {
Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
Object service = getService.invoke(null, "mount");
Class<?> Stub = Class.forName("android.os.storage.IMountService$Stub");
Method asInterface = Stub.getMethod("asInterface", Class.forName("android.os.IBinder"));
Object mountServ = asInterface.invoke(null, service);
Method unmountVolume = mountServ.getClass().getMethod("unmountVolume", String.class, boolean.class, boolean.class);
Object result = unmountVolume.invoke(mountServ, volume, true, false);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
}