GPS continuosly localize in version 2.3.5 (not in 2.2) - android

I have a program that employs GPS to localize a person every 2 minutes. For that, the code is:
private boolean flagLocalizacion = false;
private LocationListener locationListener;
private Location ultimaLocalizacion;
private LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location!=null) {
if(ultimaLocalizacion == null && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
} else if ((ultimaLocalizacion.getLatitude() != location.getLatitude()
|| ultimaLocalizacion.getLongitude() != location.getLongitude()) && flagLocalizacion) {
ultimaLocalizacion = location;
Toast.makeText(getApplicationContext(), "Longitude: " + location.getLongitude() + "Latitude: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
System.out.println("gps");
}
public void onProviderDisabled(String provider) {
System.out.println("No gps");
}
}; Button botonComenzar = (Button) findViewById(R.id.bComenzar);
botonComenzar.setOnClickListener(new OnClickListener() {
#SuppressWarnings("static-access")
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 12000, 30, locationListener);
Toast.makeText(getApplicationContext(), "Activada Localizacion", Toast.LENGTH_SHORT).show();
flagLocalizacion = true;
}
});
My problem is:
This applicaction works correctly in version 2.2. But the software failed when I installed it in a phone that has 2.3.5., the The fail is that localice continually, not every 2 minutes. Do you have thoughts on why that is happening?
Thanks you.

Continuous GPS refresh (i.e., with an interval between GPS updates of 1 sec), regardless of the requested minTime, is a known issue with many Android phones prior to JellyBean.
Full discussion of this issue with the Android team is here:
https://android-review.googlesource.com/#/c/34230/
A new CTSVerifier test has been added in Android 4.1 JellyBean that should prevent this from happening on JellyBean and higher.
For devices pre-Jelly Bean that are affected by this, your only option for a workaround is to manually unregister and re-register the LocationListener at whatever time interval you want GPS to refresh.

Related

How to use Geolocation API of google on itel A16 device?

I have trouble getting latitude and longitude on Itel A16 device (Android 8.1.0). I have used the following code, which works fine based on my test on other devices and emulators. But not on Itel A16 I would appreciate any help to fix this so that the Toast can show device location.
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getContext().LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
double MyLat = location.getLatitude();
double MyLong = location.getLongitude();
Toast.makeText(getContext(), " latitude: " + MyLat + " longitude: " + MyLong, Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
//Location test
if (checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(getContext(),Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
I fixed the issue with FusedLocationProviderClient , following Android Developers instructions

How to get the Latitude and Longitude when GPS is not Available?

I have implemented an android application using GoogleApiClient which sends the Location updates after every 5m displacement and it is working fine.
Consider sometimes will not get Gps Signal.In such situation how can we get the update can anyone please help me how to handle this situation?
try this,
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
the complete tutorial can be found here
please have a look this link.
and i used this code to fetch lat long:
public void getCurruntLocation() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
and
private class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
latLng = location.getLatitude() + "," + location.getLongitude();
Toast.makeText(RecentActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
and simple call getCurruntLocation() where you want to fetch.
GoogleApiClient uses a LocationRequest class for setting number of parameters. One of them - .setPrioryty. If you are using LocationRequest.PRIORITY_HIGH_ACCURACY in some cases googleApiClient won't call onLocationChanged() without gps, due to lack of accuracy of GPRS coordinates.

Android Location Services slow on tablet

I have the following code that takes around 1.5 minutes to find a location on two tablets that I've tested on, and it also takes around 30 seconds to load on a phone without GSM/CDMA enabled. Does anyone have any ideas why this takes so long to run without cell service? I have declared the class containing this code to be a LocationListener.
Here is my code:
This is in my OnCreate method:
locationNotificationBus = new EventBus();
locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy( Criteria.ACCURACY_COARSE );
String provider = locationManager.getBestProvider( locationCriteria, false );
locationManager.requestLocationUpdates(provider,0,0,this);
This is declared inside of the class itself:
#Override
public void onLocationChanged(Location location) {
Log.wtf( "LOCATOR: ", "Location Has Changed." );
Log.d("New Location", "Lat: " + location.getLatitude() + " Long: " + location.getLongitude());
locationManager.removeUpdates(this);
locationNotificationBus.post(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("LOCATOR: ", "Status Has Changed.");
}
#Override
public void onProviderEnabled(String provider) {
Log.d("LOCATOR: ", "Provider enabled.");
}
#Override
public void onProviderDisabled(String provider) {
Log.d("LOCATOR: ", "Provider disabled.");
}
It is my understanding that Coarse location can also use Wi-Fi to determine location. Is this not the case? If not, how do I go about using Wi-Fi?

Get user location LAT LNG using GPS and network

I am trying to get lat and lng and want to show that in a text box I want both mean Network address and GPS address so I have done this But every time I am getting only one address at a time
public class GetLocationMainActivity extends Activity {
double nlat;
double nlng;
double glat;
double glng;
LocationManager glocManager;
LocationListener glocListener;
LocationManager nlocManager;
LocationListener nlocListener;
TextView textViewNetLat;
TextView textViewNetLng;
TextView textViewGpsLat;
TextView textViewGpsLng;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_location_main);
//All textView
textViewNetLat = (TextView)findViewById(R.id.textViewNetLat);
textViewNetLng = (TextView)findViewById(R.id.textViewNetLng);
textViewGpsLat = (TextView)findViewById(R.id.textViewGpsLat);
textViewGpsLng = (TextView)findViewById(R.id.textViewGpsLng);
}
#Override
public void onDestroy() {
//Remove GPS location update
if(glocManager != null){
glocManager.removeUpdates(glocListener);
Log.d("ServiceForLatLng", "GPS Update Released");
}
//Remove Network location update
if(nlocManager != null){
nlocManager.removeUpdates(nlocListener);
Log.d("ServiceForLatLng", "Network Update Released");
}
super.onDestroy();
}
//This is for Lat lng which is determine by your wireless or mobile network
public class MyLocationListenerNetWork implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
nlat = loc.getLatitude();
nlng = loc.getLongitude();
//Setting the Network Lat, Lng into the textView
textViewNetLat.setText("Network Latitude: " + nlat);
textViewNetLng.setText("Network Longitude: " + nlng);
Log.d("LAT & LNG Network:", nlat + " " + nlng);
}
#Override
public void onProviderDisabled(String provider)
{
Log.d("LOG", "Network is OFF!");
}
#Override
public void onProviderEnabled(String provider)
{
Log.d("LOG", "Thanks for enabling Network !");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
public class MyLocationListenerGPS implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
glat = loc.getLatitude();
glng = loc.getLongitude();
//Setting the GPS Lat, Lng into the textView
textViewGpsLat.setText("GPS Latitude: " + glat);
textViewGpsLng.setText("GPS Longitude: " + glng);
Log.d("LAT & LNG GPS:", glat + " " + glng);
}
#Override
public void onProviderDisabled(String provider)
{
Log.d("LOG", "GPS is OFF!");
}
#Override
public void onProviderEnabled(String provider)
{
Log.d("LOG", "Thanks for enabling GPS !");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
public void showLoc(View v) {
//Location access ON or OFF checking
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
boolean networkWifiStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.NETWORK_PROVIDER);
//If GPS and Network location is not accessible show an alert and ask user to enable both
if(!gpsStatus || !networkWifiStatus)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(GetLocationMainActivity.this);
alertDialog.setTitle("Make your location accessible ...");
alertDialog.setMessage("Your Location is not accessible to us.To give attendance you have to enable it.");
alertDialog.setIcon(R.drawable.warning);
alertDialog.setNegativeButton("Enable", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
});
alertDialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Toast.makeText(getApplicationContext(), "Remember to give attandance you have to eanable it !", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
alertDialog.show();
}
//IF GPS and Network location is accessible
else
{
nlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
nlocListener = new MyLocationListenerNetWork();
nlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000 * 1, // 1 Sec
1, // 1 meter
nlocListener);
glocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
glocListener = new MyLocationListenerGPS();
glocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000 * 1, // 1 Sec
1, // 1 meter
glocListener);
}
}
}
Have you enable both NETWORK-LOCATION and GPS-LOCATION in Settings yet? As far as I know for Android ICS (4.x) and newer, due to the Security issue, you can't enable/disable location access programmatically. Try to test it on Android 2.x. Hope this helps.
You can use the new fused provider to get the user location quickly without confuse.
Location issues has been simplified with the new Google Play Services such as choosing best provider automatically depending on your priority.
Here is an example request;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(20000) // 20 seconds
.setFastestInterval(16) // 16ms = 60fps
.setNumUpdates(4)
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
Make sure you updated the Google Play Services in the SDK Manager and check out this link to get info about how to get users location with the new Play Services.
http://developer.android.com/training/location/retrieve-current.html

android:I can not stop GPS update

My app find out current location using GPS. It is working fine in outdoor. But where GPS is not available or poor it is trying to get update again and again and it drains battery. So i want to stop update when GPS is poor or unavailable. You may suggest to use
lm.removeUpdates(locationlistenerforGPS);
It is working fine when GPS is available not in indoor. I like to stop update when GPS is poor or unavailable.
my code is
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationlistenerforGPS = new mylocationlistenerGPS();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistenerforGPS);
My locationlistenerGPS() function is
private class mylocationlistenerGPS implements LocationListener {
#Override
public void onLocationChanged(Location location) {
counterGPS++;
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + " ");
Log.d("LOCATION CHANGED", location.getLongitude() + " ");
Toast.makeText(LocationActivity.this,"latitude: "+
location.getLatitude() + "longitude: " + location.getLongitude()
+ " Provider:" + location.getProvider() + " Accuracy:" + location.getAccuracy(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Thank you very much for any kind of assistance.
Don't use minTime and minDistance set to zero in requestLocationUpdates(). See requestLocationUpdates() documentation.
Bundle extras in onStatusChanged() may include satellites - the number of satellites used to derive the fix, this way you can define poor signal or you can use NmeaListener if you need additional information from GPS.

Categories

Resources