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

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

Related

Getting Location Memory Leak

I am developing an app that gets the user's location.
It works fine.
The issue is that I have a memory leak.
I am not exactly sure why and I think it's from the Location Services but I am not sure.
I have added Leak Canary to my app and it has shown me that I have a 74 mb leak from: android.hardware.SystemSensorManager$SensorEventQueue.mListener
Other times it is from com.edon.freiner.siddur.MainActivity$26.this$0 //that is my app
(anonymous implementation of android.locaition.LocationListener)
I am not sure where to look in my app because I commented out all my Location code to see if there was a change in the memory graph in Android Studio, I could not find any changes.
Here is my code:
public void startLocation() {
Log.d("Start Location", " Started");
LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
DataVariables.getInstance().setLatitude(location.getLatitude());
DataVariables.getInstance().setLongitude(location.getLongitude());
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("Loc Manager latitude", "`" + location.getLatitude());
Log.d("Loc Manager longitude", "`" + location.getLongitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,
1000, mLocationListener);
}
}
}

Android Location Update not returning

I am trying to get android location , I have this piece of code yet location is never returned. I have searched on stack overflow.
I found this bug : https://code.google.com/p/android/issues/detail?id=57707
But there is no fix till now, or i couldnt find one.
This is the code. Any help would be much appreciated
Criteria criteria = new Criteria();
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = lm.getBestProvider(criteria, true);
Location l = lm.getLastKnownLocation(provider);
final String date = fmtForPresenter.print(dt);
if(l==null) {
lm.requestLocationUpdates(provider, 100, 200,
new android.location.LocationListener() {
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
Log.e("PROVIDER", "PRO: " + provider);
}
#Override
public void onProviderDisabled(String provider) {
Log.e("PROVIDER", "PRO: " + provider);
}
#Override
public void onLocationChanged(final Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
presenter.getFoot(userProfile.getToken(),"public",lat,lng,date);
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)) {
lm.removeUpdates(this);
}
}
});
}
else {
lat = l.getLatitude();
lng = l.getLongitude();
presenter.getFoot(userProfile.getToken(),"public",lat,lng,date);
}
EDIT:
If i restart the phone location works again but i dont take this as a solution since i cant tell all my clients to restart their phones everytime there is a problem
Try the blow code to get the location...and also ensure that location services are enabled in the device
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1000, this); #Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 8);
mMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}

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.

Issue in finding the lat and long in samsung S3

I am not getting the lat and long in samsung S3.In other devices my code is working fine and getting the proper lat and long.Below is my code to get the lat and long.
public class MainActivity extends Activity {
TextView textView1;
Location currentLocation;
double currentLatitude,currentLongitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
findLocation();
}
public void findLocation() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location,currentLatitude,currentLongitude);
Toast.makeText(
MainActivity.this,
String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 5000)
.show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
void updateLocation(Location location,double currentLatitude,double currentLongitude) {
currentLocation = location;
this.currentLatitude = currentLocation.getLatitude();
this.currentLongitude = currentLocation.getLongitude();
textView1.setText(String.valueOf(this.currentLatitude) + "\n"
+ String.valueOf(this.currentLongitude));
}
}
In above code the onLocationChanged() method is not executing in S3 and the same code is not working in Samsung S3 device.In other devices it working fine.I am getting the correct lat and long in other devices.Please help me to sort out this problem..

GPS continuosly localize in version 2.3.5 (not in 2.2)

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.

Categories

Resources