在Android中挂载加密的obb apk扩展文件

时间:2013-11-30 09:50:49

标签: android encryption apk-expansion-files

我使用jobb工具创建了一个加密的.obb文件。我使用以下代码来安装obb文件:

    public void mountExpansion() {
    final StorageManager storageManager  = (StorageManager) getContext()
        .getSystemService(Context.STORAGE_SERVICE);


    String packageName = "name.of.the.package";
    String filePath = Environment.getExternalStorageDirectory()
            + "/Android/obb/" + packageName + "/" + "main."
            + version + "." + packageName + ".obb";
    final File mainFile = new File(filePath);
    if (mainFile.exists()) {
        Log.d("STORAGE", "FILE: " + filePath + " Exists");
    } else {
        Log.d("STORAGE", "FILE: " + filePath + " DOESNT EXIST");
    }

    String key = "thisIsMyPassword";
    if (!storageManager.isObbMounted(mainFile.getAbsolutePath())) {
        if (mainFile.exists()) {
            if(storageManager.mountObb(mainFile.getAbsolutePath(), key,
                    new OnObbStateChangeListener() {
                        @Override
                        public void onObbStateChange(String path, int state) {
                            super.onObbStateChange(path, state);
                            Log.d("PATH = ",path);
                            Log.d("STATE = ", state+"");
                            expansionFilePath = storageManager.getMountedObbPath(path);
                            if (state == OnObbStateChangeListener.MOUNTED) {
                                expansionFilePath = storageManager
                                        .getMountedObbPath(path);
                                Log.d("STORAGE","-->MOUNTED");
                            } 
                            else {
                                Log.d("##", "Path: " + path + "; state: " + state);
                            }
                        }
                    }))
            {
                Log.d("STORAGE_MNT","SUCCESSFULLY QUEUED");
            }
            else
            {
                Log.d("STORAGE_MNT","FAILED");
            }

        } else {
            Log.d("STORAGE", "Patch file not found");
        }
    }



}

我得到以下输出: FILE:filePath存在 成功排队

onObbStateChangeListener 内部没有任何内容被调用。我从自定义视图调用此函数并在Nexus 4 / KitKat上进行测试。

这种行为可能是什么原因?

2 个答案:

答案 0 :(得分:7)

我知道这个问题已经过时了,但这可能有助于其他人。

StorageManager将侦听器存储在弱引用中,这意味着,给定您的示例代码(在方法调用中创建的匿名实例),它几乎在您创建它之后就会消失,并且通常在安装完成之前就已经消失。您必须在自己的代码中维护对侦听器对象的引用,直到不再需要它为止。

这样的事情应该有效:

public class MyClass {

    ...
    private OnObbStateChangeListener mListener =
                new OnObbStateChangeListener() {
                    @Override
                    public void onObbStateChange(String path, int state) {
                       // your code here
                    }
                };

    public void mountExpansion() {
        ...
        if (storageManager.mountObb(mainFile.getAbsolutePath(), key, mListener)
        {
            Log.d("STORAGE_MNT","SUCCESSFULLY QUEUED");
        }
        else
        {
            Log.d("STORAGE_MNT","FAILED");
        }
        ...
     }
     ...
}

这个特殊的obb装置的怪癖已经存在,因为至少我知道蜂窝。

答案 1 :(得分:1)

使用KitKat引入的OBB安装似乎存在错误。目前还没有已知的解决方法,但应该通过下一次增量更新来修复它。

http://code.google.com/p/android/issues/detail?id=61881