Imountservice挂载/卸载量不工作的Andr​​oid 4.4 [英] Imountservice to mount/unmount volume not working on 4.4 Android

查看:656
本文介绍了Imountservice挂载/卸载量不工作的Andr​​oid 4.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用反射来挂载/卸载外部storage.it正在下面4.4 API。
code是低于

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

任何解决办法得到它working.As它抛出安全Exception.android.permission.mount_unmount_filesystems requires.I已在manifest.I decleared这个谷歌已经对这个问题,我发现允许具有系统|在签名保护level.Thanks前进。

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.

推荐答案

在为了使用签名的东西|系统权限,你的包必须通过平台的签名密钥签名。除非你正在创建自己的自定义ROM或有一个根深蒂固的设备,你就无法做到这一点。

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.

如果您的应用程序是一个普通的第三方应用程序(在Play商店发布的),那么你应该只使用公共API,而不是依赖于反思。只有公共的API的Andr​​oid被认为是稳定和曝光。他人隐藏,因为它们仅旨在由系统的内部使用。

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.

这篇关于Imountservice挂载/卸载量不工作的Andr​​oid 4.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆