I am developing a map program. I am more willing to use this method than using XML to write Android interfaces,
Instead of using the MapView provided by Android, you can use the Google Maps JavaScript API that you have been familiar with before,
Load HTML code in Android WebView, and use the powerful interaction between Java and Js provided by Android,
Use web pages as interfaces to develop programs. You can also use Js to call back the machine's local functions, such as GPS.
The following is an example of the Code:
1 package com. aloong. map;
2
3 import android. app. Activity;
4 import android. OS. Bundle;
5 import android. OS. Handler;
6 import android. webkit. WebSettings;
7 import android. webkit. WebView;
8
9 public class MyMap extends Activity {
10
11 private static String TAG = MyMap. class. getName ();
12 private WebView mWebView;
13 private Handler mHandler;
14 private WebSettings mWebSettings;
15
16/*** // ** Called when the activity is first created .*/
17 @ Override
18 public void onCreate (Bundle savedInstanceState ){
19 super. onCreate (savedInstanceState );
20 setContentView (R. layout. main );
21
22 mWebView = (WebView) this. findViewById (R. id. webview );
23 mHandler = new Handler ();
24
25 // set support for JavaScript and so on
26 mWebSettings = mWebView. getSettings ();
27 mWebSettings. setJavaScriptEnabled (true );
28 mWebSettings. setBuiltInZoomControls (true );
29 mWebSettings. setLightTouchEnabled (true );
30 mWebSettings. setsuppzoom zoom (true );
31 mWebView. setHapticFeedbackEnabled (false );
32 // mWebView. setInitialScale (0); // you can set the initial size by changing this value.
33
34 // important, used to interact with the page!
35 mWebView. addJavascriptInterface (new Object (){
36 @ SuppressWarnings ("unused ")
37 public void oneClick (final String locX, final String locY) {// parameters here can be passed as js Parameters
38 mHandler. post (new Runnable (){
39 public void run (){
40 mWebView. loadUrl ("javascript: shows (" + locX + "," + locY + ")");
41}
42 });
43}
44}, "demo"); // This name is called on the page. The method is as follows:
45 // <body onClick = "window. demo. clickOnAndroid (event. pageX, event. pageY)">
46
47 final String mimeType = "text/html ";
48 final String encoding = "UTF-8 ";
49 final String html = ""; // TODO reads HTML files from the local device
50
51 mWebView. loadDataWithBaseURL ("file: // sdcard/", html, mimeType,
52 encoding ,"");
53
54}
55}
After running the preceding code, you can open your own html page in the program and implement two-way interaction between Java and JavaScript.
We can even use JQuery and other frameworks on the page to make great results, which is much easier than writing Android code.