4

我使用 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");
        }
    }



}

我得到以下输出: 文件:filePath Exists SUCCESSFULLY QUEUED

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

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

4

2 回答 2

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 安装的这种特殊怪癖至少在我所知的蜂窝中就已经存在。

于 2015-01-09T06:24:46.877 回答
1

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

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

于 2013-12-06T21:20:07.737 回答