Can't access AAssetManager in native code passed from Java in WallpaperService

前端 未结 1 1746
别跟我提以往
别跟我提以往 2020-12-17 16:10

I\'m trying to access the the assets in native code from a custom WallpaperService. The native code compiles and works but trying to get the AAssetManager reference from th

相关标签:
1条回答
  • 2020-12-17 16:48

    Read the comments inside asset_manager_jni.h: "Note that the caller is responsible for obtaining and holding a VM reference to the jobject to prevent its being garbage collected while the native object is in use."

    In Java, you are passing an object (mgr) that may be freed by the garbage collector once the native callback is called. To prevent this, you could, for example, create the mgr variable as a private attribute in your class and then pass it through the load method, such as this:

    private static native void load(AssetManager mgr);
    
    private AssetManager mgr;
    
    @Override
    public void onCreate() {
      super.onCreate();
    
      mgr = getResources().getAssets();
      load(mgr);
    }
    

    Also, I think you must replace your native C++ callback with:

    void Java_com_example_android_livecubes_cube1_CubeWallpaper1_load
         (JNIEnv *env, jobject obj, jobject assetManager) 
    
    0 讨论(0)
提交回复
热议问题