public class getMyLocation {
final LocationManager locationManager = (LocationManager) MainTest.this
.getSystemService(Context.LOCATION_SERVICE);
final LocationListener locationListener = new LocationListener() {
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
provider + " is disabled!", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"Please standby..enabling " + provider,
Toast.LENGTH_SHORT).show();
// explicitly enable GPS
Intent enableGPS = new Intent(
"android.location.GPS_ENABLED_CHANGE");
enableGPS.putExtra("enabled", true);
sendBroadcast(enableGPS);
// explictly disable GPS
/*
* Intent intent = new
* Intent("android.location.GPS_ENABLED_CHANGE");
* intent.putExtra("enabled", false); sendBroadcast(intent);
*/
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
provider + " is enabled..", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
/*
* System.out.println("val of status: " + status + " provider: "
* + provider);
*/
if (status == 1) {
Toast.makeText(getApplicationContext(),
provider + " is enabled & available..",
Toast.LENGTH_SHORT).show();
System.out.println(provider + " is NOT available!");
} else {
System.out.println(provider + " is NOT available!");
}
/* progressDialogGPS.dismiss(); */
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// ORIG CODE --BELOW--
// moved lat/lon vars to top ^
myCurrentLon = location.getLongitude();
myCurrentLat = location.getLatitude();
myCurrentLon = Double.parseDouble(new DecimalFormat("##.####")
.format(myCurrentLon));
myCurrentLat = Double.parseDouble(new DecimalFormat("##.####")
.format(myCurrentLat));
Toast.makeText(getApplicationContext(),
myCurrentLat + " " + myCurrentLon, Toast.LENGTH_SHORT)
.show();
System.out.println(myCurrentLat + " " + myCurrentLon);
// --
// get best out of 2 locs. --BEGINS--
/*
* makeUseOfNewLocation(location);
*
* if(currentBestLocation == null){ currentBestLocation =
* location; }
*/
if (myCurrentLon != null && myCurrentLat != null && isOnline()) { // added
// chk
// for
// online..
gotLoc = 1;
System.out.println("OK GOTLOC == 1 !");
System.out
.println("Got your Current Location..disabling GPS to save Battery Power..");
// removing updates
// locationManager.removeUpdates(locationListener);
// explicitly turning off GPS
Intent intent = new Intent(
"android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
System.out.println("GPS disabled!");
finalLatNow = myCurrentLat;
finalLonNow = myCurrentLon;
//
// if(gotLoc == 0){
if (level >= 8 && myCurrentLon != null
&& myCurrentLat != null) {
// locationManager.removeUpdates(locationListener);
gotLoc = 1;
Intent i = new Intent(MainTest.this, Ques1.class);
i.putExtra("currUsrLon", myCurrentLon.toString()); // 2nd
// in
// seq.
i.putExtra("currUsrLat", myCurrentLat.toString()); // 1st
// in
// seq.
runOnUiThread(new Runnable() {
public void run() {
try {
// stuff here
progressDialogGPS.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println("--Removing Loc. Updates--");
remUpdates();
startActivity(i);
} else {
if (level < 8) {
showLowBattAlert();
} else {
}
if (myCurrentLon == null && myCurrentLat == null) {
// alert + GPS not locking on..do something()
} else {
}
if (!isOnline()) {
// alert + not online...do something()
showNoConnAlert();
} else {
}
}
//
/*
* } else{}
*/
} else {
System.out.println("INSIDE ELSE -- GOTLOC");
}
}
};
// locationManager.requestLocationUpdates(locationManager.getBestProvider(new
// Criteria(), true), 2000, 4, locationListener);
// enable gps everytime we request location update
/*
* Intent enableGPS = new Intent("android.location.GPS_ENABLED_CHANGE");
* enableGPS.putExtra("enabled", true); sendBroadcast(enableGPS);
*/
// ** ORIG Grequest location updates from GPS string
/*
* locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
* ONE_MIN, 4, locationListener);
*/
// ** now remove updating of co-ordinates
// locationManager.removeUpdates(locationListener);
void getUserLoc() {
if (gotLoc == 1) {
locationManager.removeUpdates(locationListener);
} else {
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_FINE);
// criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
criteria.setAltitudeRequired(false);
// criteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
runOnUiThread(new Runnable() {
public void run() {
try {
// stuff here
/*
* progressDialogGPS = ProgressDialog.show(_activity,
* null, null);
* progressDialogGPS.setContentView(R.layout.loader);
* progressDialogGPS
* .getWindow().setType(WindowManager.LayoutParams
* .TYPE_KEYGUARD_DIALOG);
*/
locationManager
.requestLocationUpdates(locationManager
.getBestProvider(criteria, true),
TEN_SECS, 4, locationListener);
// remove updates #
if (gotLoc == 1) {
locationManager.removeUpdates(locationListener);
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
/*
* try { Thread.currentThread().sleep(2000); } catch
* (InterruptedException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
void remUpdates() {
locationManager.removeUpdates(locationListener);
}
}