Integrate jcfxbl and webview to build an Android Application Service Platform

Source: Internet
Author: User
Integrate jcfxbl and webview to build an Android Application Service Platform

 

This series of articles is written by ex_net (Zhang jianbo). Please indicate the source for reprinting.

Http://blog.csdn.net/ex_net/article/details/8829348

Author: Zhang jianbo mailbox: 281451020@qq.com Phone: 13577062679 welcome to exchange calls!

 

I. background

Recently, to develop a zone-level service platform running in the android environment, the functional modules in the Platform need to be updated frequently, so we plan to use web-based development. Simply put, it is to develop a website. This website is disguised as an APK.

 

 

II. Introduction to the Smart terminal application environment

 

Iii. Advantages and Disadvantages of webapp development using HTML + Javascript

Advantages:

(1) encapsulate TCP or UDP in Java functions, and then provide JS call interfaces, so that webapps can easily use UDP or TCP to communicate with remote servers through socket;

(2) In mobile terminal (embedded) environment programming, you often need to obtain Mac, IP, device ID, and other information. If JavaScript code alone is hard to implement, the APK programming of android can be easily implemented;

(3) convenient transplantation. to port these webapps to iOS or windows, you only need to implement the "shell" again.

 

Disadvantages

 

4. Calling Java methods in APK through JS functions in Javascript

(1) the key code of the APK program webview is as follows:

webview1.getSettings().setJavaScriptEnabled(true);webview1.setWebChromeClient(new WebChromeClient());webview1.addJavascriptInterface(this, "JavaObj");

(2) JavaScript calls the key code of Java functions in APK

function btnTS_onclick(){   var obj = window.JavaObj;   obj.btn1Click();}

Program effect description:

By adding a button to the webpage, When you click this button, the user will call the event of a button of the APK itself.

The complete APK code is as follows:

package a.b;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.View.OnClickListener;import android.webkit.WebChromeClient;import android.webkit.WebView;import android.widget.Button;import android.widget.Toast;public class WebtestActivity extends Activity {/** Called when the activity is first created. */WebView webview1;Button button1;Handler handler;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.main);webview1 = (WebView) findViewById(R.id.webView1);button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener() {            public void onClick(View v) {            Toast.makeText(getApplicationContext(), "button1 is click",                 Toast.LENGTH_SHORT).show();            }        });handler = new Handler() {        public void handleMessage(Message msg) {            switch (msg.what) {            case 0:{            button1.performClick();            break;            }            }        }    };webview1.getSettings().setJavaScriptEnabled(true);webview1.setWebChromeClient(new WebChromeClient());webview1.addJavascriptInterface(this, "JavaObj");webview1.loadUrl("http://192.168.0.22/test.html");}    public void btn1Click() {       Message msg = new Message();    msg.obj = "";  msg.what = 0;handler.sendMessage(msg);      } }

After this APK is started, a javaobj object will be registered in webview. After registration, the page test.html will be automatically opened.

On the test.html page, call the btnts_onclick () function to call btn1click () in the APK ().

Function btnts_onclick ()

{

VaR OBJ = Window. javaobj;

OBJ. btn1click ();

}

 

5. The effect is as follows:

 

 

 

Vi. Precautions

When calling the GUI control in the APK through JavaScript, you must first bind an event code, and then JS indirectly calls it through the Event code of the registered object.

 

 

More cases are coming soon...

 

(1) Example of obtaining system information

(2) UDP case

(3) TCP case

(4) jcfxbl interface call Case

 

Recommended reading:

Installation and configuration of the andoird simulator in x86 Environment

Install Android 4.2.2 x86 http://blog.csdn.net/kunoy/article/details/8768205 with virtualbox

About Android x86 startup parameter settings http://blog.csdn.net/kunoy/article/details/8783602

 

 

 

 

Related Article

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.