android 反射调用实现mount/unmout 自动弹出、装载功能

来源:互联网 发布:淘宝通知买家虚假交易 编辑:程序博客网 时间:2024/06/05 13:31

经过了多天的努力,终于把这该死的功能做出来了,同时也掉了太多坑了,不说这么多,直接上代码!!!!


public void unMount() {        try {            Log.v("DWXD", "issfvs");            StorageManager mSD = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);            List<Object> list = (List<Object>) StorageManager.class.getMethod("getVolumes").invoke(mSD);            Log.v("DWXD", "list" + list.toString());            for (int i = 0; i < list.size(); i++) {                Object volume = list.get(i);                if (volume != null) {                    String id = (String) Class.forName("android.os.storage.VolumeInfo").getMethod("getId").invoke(volume);                    Log.v("DWXD", "is " + id);                    int type = (int) Class.forName("android.os.storage.VolumeInfo").getMethod("getType").invoke(volume);                    Log.v("DWXD", "is " + type);                    // public                    if (type == 0) {                        StorageManager.class.getMethod("unmount", String.class).invoke(mSD, id);                    }                }            }        } catch (Exception e) {            e.printStackTrace();            Log.v("DWXD", e.getStackTrace().toString());        }    }    public void Mount() {        try {            Log.v("DWXD", "issfvs");            StorageManager mSD = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);            List<Object> list = (List<Object>) StorageManager.class.getMethod("getVolumes").invoke(mSD);            Log.v("DWXD", "list" + list.toString());            for (int i = 0; i < list.size(); i++) {                Object volume = list.get(i);                if (volume != null) {                    String id = (String) Class.forName("android.os.storage.VolumeInfo").getMethod("getId").invoke(volume);                    Log.v("DWXD", "is " + id);                    int type = (int) Class.forName("android.os.storage.VolumeInfo").getMethod("getType").invoke(volume);                    Log.v("DWXD", "is " + type);                    // public                    if (type == 0) {                        StorageManager.class.getMethod("mount", String.class).invoke(mSD, id);                    }                }            }        } catch (Exception e) {            e.printStackTrace();            Log.v("DWXD", e.getStackTrace().toString());        }    }}

阅读全文
0 0
原创粉丝点击