Hybrid Mixed Development – Basic Reserve – 1

As a beginner who has just started mixing, he has no knowledge of open source hybrid frameworks, and is even more familiar with all kinds of html and js Confused, I want to say that the road ahead is destined to be bumpy. . .

Well, let’s start with the familiar ones:

1. Android uses webview to achieve hybrid development, about The first step is to prepare a webview layout

You can set a webview layout to occupy the entire terminal The screen, of course, needs to be laid out according to different needs. For example, I just added a title @+id/relativelayout

2, create an activity to add the layout file< /p>

3. Permission issues (webview). At first glance, it is a baby who needs to connect to the Internet. . .

 

4. About the real Code

package com.testdemo.sun.test001;import android.app.AlertDialog;import android.content.Context ;import android.content.DialogInterface;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.webkit.JavascriptInterface;import android. webkit.JsResult;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;public class MainActivity extends AppCompatActivity {private static final String TAG = MainActivity.class.getSimpleName(); private WebView wv_test; private static final String URL = "http://10.1.41.36:8888/notice/mobilelist.jsp"; private Button btn_back; private Button btn_forward; private Button btn_zoomin; private Button btn_zoomout; private Button btn_title_back; private Button btn_publish; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv_test = (WebView) findViewById(R.id.test001_webView) ); initView(); WebSettings wbSettings= wv_test.getSettings(); wbSettings.setJavaScriptEnabled(true); If the webView requires the user to manually enter the user name, password or other, the webview must be set to support the acquisition of gesture focus wv_test.requestFocusFromTouch(); wv_test.setWebViewClient(new MyWebViewClient());//Mainly to monitor the connection in the page and call the mobile phone The browser directly accesses wv_test.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); //Interaction with the JS interface, demo replaces the pop-up window of the original js, you can override wv_test.setWebChromeClient(new WebChromeClient());
        //Server and app data interaction JsObjection js = new JsObjection();//Get the data submitted by the server//JsObjection string is the reference of JsObjection object, alias for interactive access (implement js call native method) wv_test.addJavascriptInterface( js, "JsObjection"); //wView.loadUrl("file:///android_asset/index.html"); // -----Open the index.html file in the asset directory in this package//wView. loadUrl("content://com.android.htmlfileprovider/sdcard/index.html"); // -----Open the index.html file in the local SD card//wView.loadUrl("http://wap .baidu.com"); // -----Open the html file of the specified URL//wv_test.loadUrl("file:///android_asset/html/27-1.html"); //syncCookie(this, URL, "2253D1DCA304AE7DB96ABE7664EABDDC"); wv_test.loadUrl(URL);} public class JsObjection {@JavascriptInterface public String getMessage(String name, String pwd) {return name + ":" + pwd;}} @Override public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && wv_test.canGoBack()) {wv _test.goBack(); return true;} return super.onKeyDown(keyCode, event);} // Monitor all clicked links, and if we intercept what we need, we will jump to the corresponding page. public class MyWebViewClient extends WebViewClient {@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {wv_test.loadUrl(url); return true;} @Override public void onPageFinished(WebView view, String url) {view.getSettings().setJavaScriptEnabled (true); super.onPageFinished(view, url);} }}

Added methods that can be overridden in webviewClient and the meaning of each method:

1, received HttpRequested event
onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)
p>

2, the event before opening the link
public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url) ; return true;}

This function we can do many operations, for example, we read some special URL, so you can cancel this operation without opening the address, and perform other pre-defined operations, which is very necessary for a program.

3
, the event when the page is loaded
public void onPageFinished(WebView view, String url){ }

For the same reason, we know that a page is loaded, so we can closeloadingbar, switch program action.

4
, the event at the beginning of the loading page
public void onPageStarted(WebView view, String url, Bitmap favicon) {}

This event is called when the page is loaded, usually we can set one hereloading page, tell The user program is waiting for a network response.


This is the content that needs to be organized. Next, we will solve session retention and avoid repeated logins.

Leave a Comment

Your email address will not be published.