簡體   English   中英

ImountService無法在Android 4.4上安裝/卸載卷

[英]Imountservice to mount/unmount volume not working on 4.4 Android

我已經使用反射來安裝/卸載外部存儲。它在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
    }
}

使它工作的任何解決方法。由於拋出Security Exception.android.permission.mount_unmount_filesystems所需。我已在清單中將其清除。我在google上發現了此問題,我發現該權限具有系統簽名保護級別。在此先感謝您。

為了使用帶有signature | system東西signature | system 您的軟件包的signature | system權限必須使用平台的簽名密鑰進行簽名。 除非您要創建自己的自定義ROM或擁有根設備,否則將無法執行此操作。

如果您的應用是常規的第三方應用(在Play商店中發布),則您應僅使用公共API,而不必依賴反射。 僅公開的Android API被認為是穩定且公開的。 其他的則被隱藏,因為它們僅旨在供系統內部使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM