#####"java Initialize the loading configuration of the activity webview"/***The main methods for initializing activities*This method sets the main layout, checks and requests external storage permissions, configures WebView, and initializes view components*The instance state saved by @ paramsavedInstanceState is used when the activity is recreated*/protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Check and request external storage permissionsif(Build.VERSION.SDK_INT>=23&&checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){requestPermissions(newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);}// This line must be retained to create an external file directorygetExternalFilesDir("").getAbsolutePath();// Initialize the startup view and set the click listenerlauncherView=findViewById(R.id.launch_view);// Set the click listener for the launcher viewlauncherView.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){// When a click event occurs, call the loading WebView method of the main activityMainActivity.this.loadWebView();}});// Configure WebView settingswebView=findViewById(R.id.web_view);// Enabling JavaScript support is necessary for the interactive functionality of modern web pageswebView.getSettings().setJavaScriptEnabled(true);// Allow access to file resources so that WebView can load the content of local file protocolswebView.getSettings().setAllowFileAccess(true);// Allow access to content resources, i.e. allow loading of HTTP/HTTPS protocol contentwebView.getSettings().setAllowContentAccess(true);// Enable DOM storage so that web pages can use local storage functionalitywebView.getSettings().setDomStorageEnabled(true);// Set up WebChrome Client to enhance the functionality of WebViewwebView.setWebChromeClient(newWebChromeClient());// Custom WebViewClient to handle page loading eventswebView.setWebViewClient(newWebViewClient(){// Define a boolean variable to mark whether the page has finished loadingBooleanpageFinished=false;/** * Called when the page starts loading * @param view WebView control * @param url The URL being loaded * @param favicon The website's icon */@OverridepublicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon){Log.i(TAG,"onPageStarted");// Set the loading mark to false when the page starts loadingpageFinished=false;super.onPageStarted(view,url,favicon);}/** * Called when the page has finished loading * @param view WebView control * @param url The URL that has finished loading */@OverridepublicvoidonPageFinished(WebViewview,Stringurl){Log.i(TAG,"onPageFinished"+view.getProgress());// Check if the page has completely finished loading and has not yet been marked as finishedif(view.getProgress()==100&&pageFinished==false){// Start a new threadnewThread(newRunnable(){@Overridepublicvoidrun(){try{// Sleep the thread for 1 secondThread.sleep(1000*1);}catch(Exceptione){e.printStackTrace();}// Run code on the UI thread, typically used for updating the UIrunOnUiThread(newRunnable(){@Overridepublicvoidrun(){// Hide launcherView, e.g., loading indicatorlauncherView.setVisibility(View.GONE);}});}}).start();// Set the page loading completion mark to truepageFinished=true;}super.onPageFinished(view,url);}});// Register a JavaScript interface, allowing JavaScript to call Android methodswebView.addJavascriptInterface(MainActivity.this,"app");// Initialize the WebView loading operationthis.loadWebView();// Set an onClick listener for the button to call a JavaScript method in the WebViewfindViewById(R.id.btn).setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){// Call the JavaScript function 'callJSHello' with a random number as an argumentwebView.loadUrl("javascript:callJSHello("+Math.random()+")");}});// Log when the Activity is createdLog.i(TAG,"onCreate");}
#####"java This method is mainly responsible for displaying WebViews and loading specific URLs to showcase the functionality of the SDK"/** * In practical applications, one can experience and interact with the functions provided by the SDK by loading specific URLs */privatevoidloadWebView(){// Set launcherView to visible, possibly to indicate the loading process of the WebViewlauncherView.setVisibility(View.VISIBLE);// The following is a comment on how to use the WebView// Run this project https://github.com/AutoxingTech/AX_SDK1.0_Example,// and then use webView.loadUrl("https://xxxxx/sdk/v1.0/example") to experience SDK functionality// file:///android_asset/dist/index.html is a demo of Android and JS interaction// Load a local HTML file to showcase SDK functionality and the interaction example between Android and JSwebView.loadUrl("file:///android_asset/dist/index.html");}
#####"The main purpose of Java is to demonstrate how to call Android (Java) methods from JavaScript code for interaction"// The main purpose of this Java method is to demonstrate how to call Android (Java) methods from JavaScript code./** * This method's main purpose is to demonstrate how to call Android (Java) methods from JavaScript code. * @param args The parameters passed from the JavaScript method, processed as input strings. * @return Returns a result string concatenated with the input parameters, a random number, and a fixed string. */@android.webkit.JavascriptInterfacepublicStringactionFromJsHello(Stringargs){returnargs+Math.random()+"android_return_result";}/** * This method's main purpose is to trigger a webpage refresh operation in JavaScript code. * It accomplishes this by executing the refresh operation on the Android main thread. */@android.webkit.JavascriptInterfacepublicvoidactionFromJsWebRefresh(){Log.i(TAG,"Refreshing webpage");runOnUiThread(newRunnable(){@Overridepublicvoidrun(){MainActivity.this.loadWebView();}});}
<!DOCTYPE html><htmllang="en"><head> // 引入js-sdk
<scriptsrc=./autoxing-robot-js-sdk1.0.81.js></script><script>// appId You can apply to the relevant operational personnel to provideconstappId="—————————————"// appSecret You can apply to the relevant operational personnel to provideconstappSecret="——————————————————————————————"// robot snconstrobotId="xxxxxxxxxxx817"// Initialization, parameters include application ID, application key, and application mode, set to wide area application mode here***AppMode**-[AppMode](../Define/Define-AppMode)letaxRobot=newAXRobot(appId,appSecret,AppMode.WAN_APP);// initfunctioninit(){axRobot.init().then((isOk)=>{//initreturnaxRobot.connectRobot({//connectRobotrobotId:robotId})}).then((isOK)=>{returnaxRobot.getPoiList();//get poiList}).then((res)=>{console.log(res)}).catch((res)=>{console.log(res.errText)})}// Android calls JavaScript's Hello function @ param {string} text - string parameter passed from AndroidfunctioncallJSHello(text){// sdk initinit()// Get the element with ID 'androidContent', display the value passed in from Android, and add prompt text before itdocument.getElementById("androidContent").innerHTML="Android:"+text}// js Call the methods provided by AndroidfunctionclickAndroidHello(){//app from: webView.addJavascriptInterface(MainActivity.this, "app");letandroid_res=app.actionFromJsHello("你好")// By calling the Android method, the return value is displayed in the element with ID "androidContent"document.getElementById("androidContent").innerHTML=android_res}// JavaScript calls Android method to refresh WebViewfunctionclickAndroidRefresh(){//Call the Android native app.actionFromJsWebRefresh method to perform a WebView refresh operationapp.actionFromJsWebRefresh()}</script></head><body><divstyle="margin-top:50px;font-weight:bold">111Android WebView 交互Demo</div><divid="androidContent"></div><buttononclick="clickAndroidHello()">调用Android代码</button><buttononclick="clickAndroidRefresh()">调用Android刷新webView</button></body></html>