Android Asynctask & Locationlistener
I created a class GPSController. The onReceive is called every Minute. I want to get the current GPS Data with the GPSListener in my AsyncTask. But the onLocationChanged Method i
Solution 1:
This class provide current lat , log and In onLocationChanged method you can call AsyncTask called every Minute.
publicclassGPSTrackerextendsServiceimplementsLocationListener {
privatestaticfinalStringTAG= GPSTracker.class.getSimpleName();
privatefinal Context mContext;
// flag for GPS statusbooleanisGPSEnabled=false;
// flag for network statusbooleanisNetworkEnabled=true;
// flag for GPS statusbooleancanGetLocation=false;
Location location; // locationdouble latitude; // latitudedouble longitude; // longitude// The minimum distance to change Updates in metersprivatestaticfinallongMIN_DISTANCE_CHANGE_FOR_UPDATES=10; // 10 meters// The minimum time between updates in millisecondsprivatestaticfinallongMIN_TIME_BW_UPDATES=1000 * 60 * 1; // 1 minute// Declaring a Location Managerprotected LocationManager locationManager;
publicGPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled) {
} else {
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
canGetLocation = true;
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// SharedPreferenceUtil.putValue(Preferences.TAG_LATITUDE,""+latitude);// SharedPreferenceUtil.putValue(Preferences.TAG_LONGITUDE,""+longitude);// SharedPreferenceUtil.save();// Preferences.TAG_G_L = "G";
Log.d("gps", latitude + "," + longitude);
}
}
} else {
canGetLocation = false;
Log.d("gps", "GPS Disable");
}
}
}
if (!isNetworkEnabled) {
// no network provider is enabled
} else {
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Preferences.TAG_G_L = "L";
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// SharedPreferenceUtil.putValue(Preferences.TAG_LATITUDE,""+latitude);// SharedPreferenceUtil.putValue(Preferences.TAG_LONGITUDE,""+longitude);// SharedPreferenceUtil.save();
Log.d("network", latitude + "," + longitude);
}
}
Log.d("network", latitude + "," + longitude);
}
}
/* if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
SharedPreferenceUtil.putValue(Preferences.TAG_LATITUDE,""+latitude);
SharedPreferenceUtil.putValue(Preferences.TAG_LONGITUDE,""+longitude);
SharedPreferenceUtil.save();
Log.d("gps",latitude+","+longitude);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
SharedPreferenceUtil.putValue(Preferences.TAG_LATITUDE,""+latitude);
SharedPreferenceUtil.putValue(Preferences.TAG_LONGITUDE,""+longitude);
SharedPreferenceUtil.save();
Log.d("gps",latitude+","+longitude);
}
}
}
}
}*/
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/publicvoidstopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
*/publicdoublegetLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitudereturn latitude;
}
/**
* function to get longitude
*
* @return double
*/publicdoublegetLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitudereturn longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
*/publicbooleancanGetLocation() {
returnthis.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
*/publicvoidshowSettingsAlert() {
AlertDialog.BuilderalertDialog=newAlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
Intentintent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@OverridepublicvoidonLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location.toString());
}
@OverridepublicvoidonProviderDisabled(String provider) {
}
@OverridepublicvoidonProviderEnabled(String provider) {
}
@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
}
@Overridepublic IBinder onBind(Intent arg0) {
returnnull;
}
}
I hope it's help to you..!
Post a Comment for "Android Asynctask & Locationlistener"