Android uses HTML Design Software Interface webview

Source: Internet
Author: User

MainActivity. java [java] package cn.itcast.html; import java. util. list; import org. json. JSONArray; import org. json. JSONException; import org. json. JSONObject; import cn. itcast. domain. contact; import cn. itcast. service. contactService; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. util. log; import android. webkit. webView; publ Ic class MainActivity extends Activity {private static final String TAG = "MainActivity"; private ContactService contactService; private WebView webView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); contactService = new ContactService (); webView = (WebView) this. findViewById (R. id. webView); webView. getSettings (). setJa VaScriptEnabled (true); webView. addJavascriptInterface (new ContactPlugin (), "contact"); // webView. loadUrl ("file: // android_asset/index.html"); webView. loadUrl ("http: // 192.168.1.100: 8080/videoweb/index.html");} private final class ContactPlugin {public void getContacts () {List <Contact> contacts = contactService. getContacts (); try {JSONArray array = new JSONArray (); for (Contact: contacts ){ JSONObject item = new JSONObject (); item. put ("id", contact. getId (); item. put ("name", contact. getName (); item. put ("mobile", contact. getMobile (); array. put (item);} String json = array. toString (); Log. I (TAG, json); webView. loadUrl ("javascript: show ('" + json + "')");} catch (JSONException e) {e. printStackTrace () ;}} public void call (String mobile) {Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + mobile); startActivity (intent) ;}} ContactService. java [java] package cn. itcast. service; import java. util. arrayList; import java. util. list; import cn. itcast. domain. contact; public class ContactService {public List <Contact> getContacts () {List <Contact> contacts = new ArrayList <Contact> (); contacts. add (new Contact (34, "Zhang Xiaoxiang", "1398320333"); contacts. add (new Contact (39, "Feng wei", "1 332432444 "); contacts. add (new Contact (67, "old Bi", "1300320333"); return contacts ;}} Contact. java [java] package cn. itcast. domain; public class Contact {private Integer id; private String name; private String mobile; public Integer getId () {return id;} public void setId (Integer id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;} Public String getMobile () {return mobile;} public void setMobile (String mobile) {this. mobile = mobile;} public Contact (Integer id, String name, String mobile) {super (); this. id = id; this. name = name; this. mobile = mobile ;}} index.html [html] <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">

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.