Как поделиться или получить доступ к объекту данных Angular 7 размещенного веб-сайта с помощью Android WebView? - PullRequest
0 голосов
/ 25 мая 2020

Я разрабатываю приложение Android с помощью WebView, оно успешно загружает веб-сайт с помощью API-интерфейса loadUrl webview. Я хочу получить доступ к объекту данных приложения Angular в коде Android. Я изучил много потоков и узнал о WebView addJavascriptInterface, и я реализовал это в коде Android, как показано ниже в MainActivity.

Это метод onCreate приложения Android, где я использую WebView

    public class MainActivity extends AppCompatActivity {
        private WebView      simpleWebView;
         NativeInterface nativeInterface = new NativeInterface(this);


protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String url = "http://192.168.43.202:4200/"; // For this code i am using localhost , finally it will be replaced by webserver URL
            NativeInterface nativeInterface = new NativeInterface(this);

            WebSettings settings = simpleWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setDomStorageEnabled(true);

            simpleWebView.addJavascriptInterface(nativeInterface, "MyInterface");
            simpleWebView.loadUrl(url); // load the url on the web view

            nativeInterface.setToken("1234");
        }
 }   

Create a NativeInterface in NativeInterface.java

public class NativeInterface {
    Context mContext;
    String  mtokenValue;

    NativeInterface(Context context) {
        mContext = context;
    }
    @JavascriptInterface
    public void setToken(String tokenValue){
        mtokenValue = tokenValue;
    }

    @JavascriptInterface
    public String getToken() {
        return mtokenValue;
    }
}

Now at Angular 7 code that is running on my localhost server outside of android studio , I did below change for accessing nativeInterface in the angular app.component.ts then did ng serve.

    interface MyInterface {
      getToken() :String 
    }
    var android : MyInterface;

    ngOnInit() {enter code here
       android.getToken();
    } 

Every time it says " I/chromium: [INFO:CONSOLE(75082)] "ERROR TypeError: Cannot read property 'getToken' of undefined", source: http://192.168.43.202:4200/vendor.js (75082 "

Is it possible to access Android object in Angular 7 code or vice-versa ? If yes then please help to acheive this data sharing.
...