简体   繁体   中英

Why can't I call a Java method of an imported android platform api class in cordova cusotm plugin

I am creating an App with Ionic and in order to load assets that are working with Play Asset Delivery I need to access the android native code by creating a cordova Plugin. I need to add those three lines of code . In order to achieve this I imported the Android Platform APIs that contain those methods and classes.

Plugins Java file:

package cordova.plugin.hello;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//My Imports
import android.content.res.AssetManager;
import android.content.Context;
import java.io.InputStream; 

public class hello extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Context context = createPackageContext("io.ionic.starter", 0);
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open("pack1");
    }
}

When building the App I get this error:

> Task :app:compileDebugJavaWithJavac FAILED
C:\Users\LUM\appAsset\platforms\android\app\src\main\java\cordova\plugin\hello\hello\hello.java:22: error: cannot find symbol
        Context context = createPackageContext("io.ionic.starter", 0);
                          ^
  symbol:   method createPackageContext(String,int)
  location: class hello
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

I am very new to app development and all possible solutions that I found on the Internet created different Errors and while going further away from the three lines of code you can see in the first linked article. For example adding this.cordova.getActivity() and call createPackageContext("io.ionic.starter", 0); on that. That created even more errors. Same with other fixing tries.

I get it that it's normal to get Errors even after fixing others, but it would really help me out if you can help me out with what I am fundamentally doing wrong with the code.

Android SDK Tools : 26.1.1
NodeJS            : v12.18.3
npm               : 6.14.6
OS                : Windows 10
Gradle            : 6.6.1
Ionic Framework   : @ionic/angular 5.3.3
Cordova Platforms : android 9.0.0

@Stultuske is right - createPackageContext() is an instance method on the android.content.Context class so you need to call it on an existing class instance. You can use the application context instance; try this:

Context context = this.cordova
        .getActivity()
        .getApplicationContext()
        .createPackageContext("io.ionic.starter", 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM