Hybird mixed-type development and construction

Source: Internet
Author: User

1.xml

<?xml version= "1.0" encoding= "Utf-8"? ><webview xmlns:android= "Http://schemas.android.com/apk/res/android "    android:id=" @+id/webview "    android:layout_width=" Fill_parent "    android: Layout_height= "Fill_parent"/>

2.activity

 PackageCom.firefly.hybirdapp;Importandroid.content.Intent;ImportAndroid.net.Uri;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportAndroid.widget.Toast; Public classHomeactivityextendsappcompatactivity {WebView mywebview; //String loadurl = "File:///android_assets/index.html "; localString Neturl = "http://172.16.46.114:14023/index.html/"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_home); Mywebview=(WebView) Findviewbyid (R.id.webview); Mywebview.loadurl (Neturl);//Load URL//Set WebViewMywebview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true;        }        }); //Get Set classWebSettings ws =mywebview.getsettings (); Ws.setjavascriptenabled (true);//Allow JS script//write the method of JS call in WebAppMywebview.addjavascriptinterface (NewMywebappinterface ( This), "Android"); }    //Press the back key to return to the previous page@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        //Check If the key event is the back button and if there's history        if((keycode = = Keyevent.keycode_back) &&Mywebview.cangoback ())            {Mywebview.goback (); return true; }        return Super. OnKeyDown (KeyCode, event); }}

3.WEBAPP class

 PackageCom.firefly.hybirdapp;ImportAndroid.content.Context;ImportAndroid.util.Log;ImportAndroid.webkit.JavascriptInterface;/*** Created by Pengdan on 2016/6/27.*/ Public classMywebappinterface {context context;  PublicMywebappinterface (Context context) { This. Context =context; } @JavascriptInterface Publicstring Lookdata (String data) {LOG.E ("TAG", Data + "hello!!"); //return information to HTML        return"Hello"; }}

4.html

<! DOCTYPE html>Mui.init (); function look () {var ua=navigator.userAgent.toLocaleLowerCase ();                Alert (UA); //Determine if Android                if(/android/. Test (UA)) {                    //Send Messagevar msg = window.android.lookData (' I am HTML5 '); //write the received message into spandocument.getElementById (' span '). InnerHTML =msg;                Alert (msg); }            }        </script> 

Hybird mixed-type development and construction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.