How to call JavaScript code from Java via JavaScript

  Kiến thức lập trình


package com.mycompany.myapph;

import android.Manifest;

import android.app.Activity;

import android.content.pm.PackageManager;

import android.view.*;

import java.io.*;

import android.os.*;

import android.webkit.*;

import android.widget.Toast;

import java.nio.file.Files;

import java.nio.file.Paths;



import android.content.Intent;

import android.net.Uri;

import android.support.v4.content.FileProvider;





public class MainActivity extends Activity {

  public   WebView webView;

    public String fileContent;







    private static final int PERMISSION_REQUEST_CODE = 1;

    public String directoryPath = "/storage/emulated/0/fork/";

    public String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/fork/B.txt";



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);

        // Hide the notification bar

        View decorView = getWindow().getDecorView();

        int flags = View.SYSTEM_UI_FLAG_FULLSCREEN;

        decorView.setSystemUiVisibility(flags);

        

    webView = findViewById(R.id.mainwebView);



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

        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient());

        webView.getSettings().setDomStorageEnabled(true);

        

        

        File directory = new File(directoryPath);

        if (!directory.exists()) {

            boolean created = directory.mkdirs();}

           

                

        

        uv();



        







        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

            if (!Environment.isExternalStorageManager()) {

                return;

            }

        } else {

            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);

                return;

            }

        }





        JavaScriptInterface jsInterface = new JavaScriptInterface(MainActivity.this);



        webView.addJavascriptInterface(jsInterface, "AndroidBridge");

MainActivity mb=new MainActivity();

        webView.addJavascriptInterface(this, "And");

        

        

        

    }







    @JavascriptInterface

public void pl(){ 

    vivo();

}







    @Override

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == PERMISSION_REQUEST_CODE) {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {



            }

        }

    }



    public void wr( String z) {



        try {

            FileWriter writer = new FileWriter(filePath);

            writer.write(z);

            writer.close();

        } catch (IOException e) {

            e.printStackTrace();

        }





    }













    public void uv() {



        try {

            fileContent= new String(Files.readAllBytes(Paths.get(filePath)));





        } 





        catch (IOException e) {



            e.printStackTrace();

        }





    }













public void viv(){

    Toast.makeText(webView.getContext(), "2828338", Toast.LENGTH_SHORT).show();

    

firstly I am a beginner in programming
I am facing a problem when I want to call a pl() function contained in this code via JavaScript, in order to in turn call the vivo() function, which sends the content of a file content to JavaScript. I tried it for a long time and searched to no avail.

I tried to call the pl() function in body of code i mean into onCreate and it sent the file content but when i try it into JavaScript the function doesn’t work w

}

public void vivo() {

    

    

    webView.setWebViewClient(new WebViewClient() {



            @Override

            public void onPageFinished(WebView view, String url) {

                super.onPageFinished(view, url);



                







                String javascript = "javascript:receiveFileContent('" + fileContent + "')";

                webView.loadUrl(javascript);}



        });

    

    }

}

New contributor

bashir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT