Travelflan Widget Android Usage Sample For China-mobile
We provides the web for chatbot services. Ultimately, we will recommend using it through the Native sdk. Until then, we recommend using the webview module in your Android app.
- Insert WebViewClientImpl, JavaScriptInterface source file in your application. If you want to know more about how to use it, please refer to the official Android document
- Declare and implement your source code refer to the usage below in your Activity.
// Declare Your Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set Widget WebView
setTFWebView();
// This is for controle webview
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(tfWebView.getVisibility() == View.GONE) {
tfWebView.setVisibility(View.VISIBLE);
} else {
tfWebView.setVisibility(View.GONE);
}
}
});
}
private void setTFWebView() {
tfWebView = (WebView) findViewById(R.id.webview);
// allow javascript
WebSettings webSettings = tfWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
tfWebView.setVisibility(View.GONE);
tfWebView.setWebViewClient(webViewClient);
tfWebView.addJavascriptInterface(new JavaScriptInterface(this, tfWebView),"androidHandler");
tfWebView.loadUrl("https://widget-cm.travelflan.com.cn");
}
@Override
public void onBackPressed() {
// block back button
if(tfWebView.canGoBack()) {
tfWebView.goBack();
} else {
// define your task!
// super.onBackPressed();
}
}
In JavaScriptInterface this script should be excute.
@Override
public void onPageFinished(WebView view, String url) {
String setScript = "window.webViewBridge.setEnv('android')";
view.loadUrl("javascript:" + setScript);
String script = "window.webViewBridge.send({type:'initialize', provider_id: 13})";
view.loadUrl("javascript:" + script);
super.onPageFinished(view, url);
}