1. public class getMyLocation {
  2.         final LocationManager locationManager = (LocationManager) MainTest.this
  3.                 .getSystemService(Context.LOCATION_SERVICE);
  4.  
  5.         final LocationListener locationListener = new LocationListener() {
  6.  
  7.             public void onProviderDisabled(String provider) {
  8.                 // TODO Auto-generated method stub
  9.                 Toast.makeText(getApplicationContext(),
  10.                         provider + " is disabled!", Toast.LENGTH_SHORT).show();
  11.                 Toast.makeText(getApplicationContext(),
  12.                         "Please standby..enabling " + provider,
  13.                         Toast.LENGTH_SHORT).show();
  14.  
  15.                 // explicitly enable GPS
  16.                 Intent enableGPS = new Intent(
  17.                         "android.location.GPS_ENABLED_CHANGE");
  18.                 enableGPS.putExtra("enabled", true);
  19.                 sendBroadcast(enableGPS);
  20.  
  21.                 // explictly disable GPS
  22.                 /*
  23.                  * Intent intent = new
  24.                  * Intent("android.location.GPS_ENABLED_CHANGE");
  25.                  * intent.putExtra("enabled", false); sendBroadcast(intent);
  26.                  */
  27.  
  28.             }
  29.  
  30.             public void onProviderEnabled(String provider) {
  31.                 // TODO Auto-generated method stub
  32.                 Toast.makeText(getApplicationContext(),
  33.                         provider + " is enabled..", Toast.LENGTH_SHORT).show();
  34.  
  35.             }
  36.  
  37.             public void onStatusChanged(String provider, int status,
  38.                     Bundle extras) {
  39.                 // TODO Auto-generated method stub
  40.                 /*
  41.                  * System.out.println("val of status: " + status + " provider: "
  42.                  * + provider);
  43.                  */
  44.                 if (status == 1) {
  45.                     Toast.makeText(getApplicationContext(),
  46.                             provider + " is enabled & available..",
  47.                             Toast.LENGTH_SHORT).show();
  48.  
  49.                     System.out.println(provider + " is NOT available!");
  50.                 } else {
  51.                     System.out.println(provider + " is NOT available!");
  52.                 }
  53.  
  54.                 /* progressDialogGPS.dismiss(); */
  55.  
  56.             }
  57.  
  58.             public void onLocationChanged(Location location) {
  59.                 // TODO Auto-generated method stub
  60.  
  61.                 // ORIG CODE --BELOW--
  62.  
  63.                 // moved lat/lon vars to top ^
  64.  
  65.                 myCurrentLon = location.getLongitude();
  66.                 myCurrentLat = location.getLatitude();
  67.  
  68.                 myCurrentLon = Double.parseDouble(new DecimalFormat("##.####")
  69.                         .format(myCurrentLon));
  70.                 myCurrentLat = Double.parseDouble(new DecimalFormat("##.####")
  71.                         .format(myCurrentLat));
  72.  
  73.                 Toast.makeText(getApplicationContext(),
  74.                         myCurrentLat + " " + myCurrentLon, Toast.LENGTH_SHORT)
  75.                         .show();
  76.                 System.out.println(myCurrentLat + " " + myCurrentLon);
  77.  
  78.                 // --
  79.  
  80.                 // get best out of 2 locs. --BEGINS--
  81.                 /*
  82.                  * makeUseOfNewLocation(location);
  83.                  *
  84.                  * if(currentBestLocation == null){ currentBestLocation =
  85.                  * location; }
  86.                  */
  87.                 if (myCurrentLon != null && myCurrentLat != null && isOnline()) { // added
  88.                                                                                     // chk
  89.                                                                                     // for
  90.                                                                                     // online..
  91.                     gotLoc = 1;
  92.                     System.out.println("OK GOTLOC == 1 !");
  93.                     System.out
  94.                             .println("Got your Current Location..disabling GPS to save Battery Power..");
  95.                     // removing updates
  96.                     // locationManager.removeUpdates(locationListener);
  97.                     // explicitly turning off GPS
  98.                     Intent intent = new Intent(
  99.                             "android.location.GPS_ENABLED_CHANGE");
  100.                     intent.putExtra("enabled", false);
  101.                     sendBroadcast(intent);
  102.                     System.out.println("GPS disabled!");
  103.  
  104.                     finalLatNow = myCurrentLat;
  105.                     finalLonNow = myCurrentLon;
  106.                     //
  107.                     // if(gotLoc == 0){
  108.                     if (level >= 8 && myCurrentLon != null
  109.                             && myCurrentLat != null) {
  110.                         // locationManager.removeUpdates(locationListener);
  111.                         gotLoc = 1;
  112.                         Intent i = new Intent(MainTest.this, Ques1.class);
  113.                         i.putExtra("currUsrLon", myCurrentLon.toString()); // 2nd
  114.                                                                             // in
  115.                                                                             // seq.
  116.                         i.putExtra("currUsrLat", myCurrentLat.toString()); // 1st
  117.                                                                             // in
  118.                                                                             // seq.
  119.  
  120.                         runOnUiThread(new Runnable() {
  121.                             public void run() {
  122.                                 try {
  123.                                     // stuff here
  124.  
  125.                                     progressDialogGPS.dismiss();
  126.                                 } catch (Exception e) {
  127.                                     e.printStackTrace();
  128.                                 }
  129.                             }
  130.                         });
  131.                         System.out.println("--Removing Loc. Updates--");
  132.                         remUpdates();
  133.                         startActivity(i);
  134.  
  135.                     } else {
  136.                         if (level < 8) {
  137.                             showLowBattAlert();
  138.                         } else {
  139.                         }
  140.                         if (myCurrentLon == null && myCurrentLat == null) {
  141.                             // alert + GPS not locking on..do something()
  142.                         } else {
  143.                         }
  144.                         if (!isOnline()) {
  145.                             // alert + not online...do something()
  146.                             showNoConnAlert();
  147.                         } else {
  148.                         }
  149.                     }
  150.                     //
  151.                     /*
  152.                      * } else{}
  153.                      */
  154.  
  155.                 } else {
  156.                     System.out.println("INSIDE ELSE -- GOTLOC");
  157.  
  158.                 }
  159.             }
  160.  
  161.         };
  162.  
  163.         // locationManager.requestLocationUpdates(locationManager.getBestProvider(new
  164.         // Criteria(), true), 2000, 4, locationListener);
  165.  
  166.         // enable gps everytime we request location update
  167.         /*
  168.          * Intent enableGPS = new Intent("android.location.GPS_ENABLED_CHANGE");
  169.          * enableGPS.putExtra("enabled", true); sendBroadcast(enableGPS);
  170.          */
  171.  
  172.         // ** ORIG Grequest location updates from GPS string
  173.  
  174.         /*
  175.          * locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
  176.          * ONE_MIN, 4, locationListener);
  177.          */
  178.  
  179.         // ** now remove updating of co-ordinates
  180.  
  181.         // locationManager.removeUpdates(locationListener);
  182.         void getUserLoc() {
  183.             if (gotLoc == 1) {
  184.                 locationManager.removeUpdates(locationListener);
  185.             } else {
  186.             }
  187.  
  188.             final Criteria criteria = new Criteria();
  189.             criteria.setAccuracy(Criteria.ACCURACY_FINE);
  190.             criteria.setHorizontalAccuracy(Criteria.ACCURACY_FINE);
  191.             // criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
  192.             criteria.setAltitudeRequired(false);
  193.             // criteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
  194.             criteria.setBearingRequired(false);
  195.             criteria.setCostAllowed(true);
  196.  
  197.             criteria.setPowerRequirement(Criteria.POWER_HIGH);
  198.  
  199.             runOnUiThread(new Runnable() {
  200.                 public void run() {
  201.                     try {
  202.                         // stuff here
  203.                         /*
  204.                          * progressDialogGPS = ProgressDialog.show(_activity,
  205.                          * null, null);
  206.                          * progressDialogGPS.setContentView(R.layout.loader);
  207.                          * progressDialogGPS
  208.                          * .getWindow().setType(WindowManager.LayoutParams
  209.                          * .TYPE_KEYGUARD_DIALOG);
  210.                          */
  211.  
  212.                         locationManager
  213.                                 .requestLocationUpdates(locationManager
  214.                                         .getBestProvider(criteria, true),
  215.                                         TEN_SECS, 4, locationListener);
  216.  
  217.                         // remove updates #
  218.                         if (gotLoc == 1) {
  219.                             locationManager.removeUpdates(locationListener);
  220.                         } else {
  221.                         }
  222.                     } catch (Exception e) {
  223.                         e.printStackTrace();
  224.                     }
  225.                 }
  226.             });
  227.  
  228.             /*
  229.              * try { Thread.currentThread().sleep(2000); } catch
  230.              * (InterruptedException e) { // TODO Auto-generated catch block
  231.              * e.printStackTrace(); }
  232.              */
  233.  
  234.         }
  235.  
  236.         void remUpdates() {
  237.             locationManager.removeUpdates(locationListener);
  238.         }
  239.     }