0

I've been developing an Android app for my business for a long time and it's been working fine, using a webview and calling back and forth between native features. In case it's relevant, here's the Java entry point to my app:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.webview);
    final Context that = this;
    myWebView.addJavascriptInterface(new WebAppInterface(this, this), "Android");

    myWebView.setWebViewClient(new WebViewClient(){
        public void onPageFinished(WebView view, String url) {
            myWebView.evaluateJavascript("pageFinished()", null);
        }
    });

    pm = this.getPackageManager();

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowUniversalAccessFromFileURLs(true);
    webSettings.setDomStorageEnabled(true);
    WebView.setWebContentsDebuggingEnabled(true);

    Window window = getWindow();
    //window.setStatusBarColor(Color.parseColor("#3596d4"));

    myWebView.loadUrl("file:///android_asset/index.html");

    IntentFilter filter = new IntentFilter();
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
    registerReceiver(myBroadcastReceiver, filter);

    mDetector = new GestureDetectorCompat(that, this);
    myWebView.setOnTouchListener(touchListener);
}

Now when I set the minimum version to 18 in my gradle file, some lines got marked by Android Studio as 'not supported by API 18', most notably my myWebView.evaluateJavascript calls, but also some other stuff like WebView.setWebContentsDebuggingEnabled.

How can I do evaluateJavascript in API 18?

Also in some places, when I call evaluateJavascript, I expect a response back, for example:

    myWebView.evaluateJavascript("Back.go()", new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String canGoBack) {
            if (canGoBack.equals("false")) {
                moveTaskToBack(true);
            } else {
                // don't need to do anything, already called Back.go()
                // that means the javascript already handled navigation
            }
        }
    });

Will that be possible with API 18?

1
  • Note, the Google Play Store is not allowing uploaded apps to have less than SDK level 24. So, if this app needs to be distributed via the App Stores, you have to set the minimum SDK level to 24 or above. Commented May 9, 2019 at 15:37

1 Answer 1

1

How can I do evaluateJavascript in API 18?

Use loadUrl("javascript:..."), where the...` is your JavaScript code.

Will that be possible with API 18?

No, sorry. The closest thing is to have your JavaScript code call something on your WebAppInterface that you injected via addJavascriptInterface().

Note that at the present time only ~4% of Android devices using the Play Store are running API Level 18 or older.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.