PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/WebView/GeoWeb1/src/com/commonsware/android/geoweb/GeoWebOne.java

http://github.com/commonsguy/cw-advandroid
Java | 95 lines | 60 code | 18 blank | 17 comment | 1 complexity | 24c029ae4ac15efd24c9a8587378ad00 MD5 | raw file
Possible License(s): Apache-2.0
  1. /***
  2. Copyright (c) 2008-2012 CommonsWare, LLC
  3. Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4. use this file except in compliance with the License. You may obtain a copy
  5. of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  6. by applicable law or agreed to in writing, software distributed under the
  7. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  8. OF ANY KIND, either express or implied. See the License for the specific
  9. language governing permissions and limitations under the License.
  10. From _The Busy Coder's Guide to Advanced Android Development_
  11. http://commonsware.com/AdvAndroid
  12. */
  13. package com.commonsware.android.geoweb;
  14. import android.app.Activity;
  15. import android.content.Context;
  16. import android.os.Bundle;
  17. import android.location.Location;
  18. import android.location.LocationListener;
  19. import android.location.LocationManager;
  20. import android.webkit.WebView;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23. public class GeoWebOne extends Activity {
  24. private static String PROVIDER=LocationManager.GPS_PROVIDER;
  25. private WebView browser;
  26. private LocationManager myLocationManager=null;
  27. @Override
  28. public void onCreate(Bundle icicle) {
  29. super.onCreate(icicle);
  30. setContentView(R.layout.main);
  31. browser=(WebView)findViewById(R.id.webkit);
  32. myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  33. browser.getSettings().setJavaScriptEnabled(true);
  34. browser.addJavascriptInterface(new Locater(), "locater");
  35. browser.loadUrl("file:///android_asset/geoweb1.html");
  36. }
  37. @Override
  38. public void onResume() {
  39. super.onResume();
  40. myLocationManager.requestLocationUpdates(PROVIDER, 10000,
  41. 100.0f,
  42. onLocation);
  43. }
  44. @Override
  45. public void onPause() {
  46. super.onPause();
  47. myLocationManager.removeUpdates(onLocation);
  48. }
  49. LocationListener onLocation=new LocationListener() {
  50. public void onLocationChanged(Location location) {
  51. // ignore...for now
  52. }
  53. public void onProviderDisabled(String provider) {
  54. // required for interface, not used
  55. }
  56. public void onProviderEnabled(String provider) {
  57. // required for interface, not used
  58. }
  59. public void onStatusChanged(String provider, int status,
  60. Bundle extras) {
  61. // required for interface, not used
  62. }
  63. };
  64. public class Locater {
  65. public String getLocation() throws JSONException {
  66. Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
  67. if (loc==null) {
  68. return(null);
  69. }
  70. JSONObject json=new JSONObject();
  71. json.put("lat", loc.getLatitude());
  72. json.put("lon", loc.getLongitude());
  73. return(json.toString());
  74. }
  75. }
  76. }