如何在Android webView的Angular应用程序中调用Android Function?
目前我正在开发Angular应用程序,它将加载到Android应用程序中的WebView。在Angular应用程序启动时,我必须从Android界面调用一个函数 Android中定义的接口:如何在Android webView的Angular应用程序中调用Android Function?,android,angular,typescript,webview,Android,Angular,Typescript,Webview,目前我正在开发Angular应用程序,它将加载到Android应用程序中的WebView。在Angular应用程序启动时,我必须从Android界面调用一个函数 Android中定义的接口: class WebAppInterface(private val mContext: Context) { @JavascriptInterface fun someFoo() { Toast.makeText(mContext, 'some text', Toast.L
class WebAppInterface(private val mContext: Context) {
@JavascriptInterface
fun someFoo() {
Toast.makeText(mContext, 'some text', Toast.LENGTH_SHORT).show()
}
}
val webView: WebView = findViewById(R.id.webview)
webView.addJavascriptInterface(WebAppInterface(this), "Android")
在角度应用程序中调用someFoo():
declare const Android;
@Injectable()
export class SomeService {
...
callAndroidFoo(): void {
try {
if (!!Android) {
Android.someFoo();
}
} catch (err) {
console.assert(err);
}
}
}
当我在WebView中重新加载已经打开的页面时一切正常,并且调用了该功能,但是在打开Android应用程序后,在WebView上第一次运行Angular应用程序时,它不起作用。
我想Angular应用程序在第一次运行时并没有检测到Android界面。
我将非常感激能找到解决这个问题的办法
export class AppComponent implements OnInit {
constructor(private someService: SomeService) {}
ngOnInit() {
this.someService.callAndroidFoo();
}
}