Android LocationManager in "Phone Only" mode - android

I am trying to use the following code snippet on an Android app with targetSdkVersion = 22. (source)
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
System.out.println(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
In my manifest permissions, I have:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
Under Settings -> Location, my phone (Galaxy S8) has three locating methods:
High accuracy, Battery Saving and Phone only. This code successfully works for the first 2 locating methods but not Phone only.
Phone only says it "Uses GPS to estimate your location", so I would expect using the GPS_PROVIDER would be sufficient to get location changes.
Why isn't it working?

Related

How to get location without internet using intent service?

I don't want to use service which is continuously running in background ! Actually in my project user will send SMS command from any smartphone to his/her misplaced smartphone.My app will detect that particular "SMS command" and in return it will send the current location of misplaced mobile.
can it be done through intent service ? I m damn confused ... Its single time operation how to perform it efficiently ... ?
by using NETWORK_PROVIDER getting geolocation
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
and add this permission in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Using LocationListener for API 21 on Marshmallow

I have an app that listens to the GPS update, which was implemented last year using API 21. It was tested on several Android phones running Lollipop, such as LG G4. Now I installed it on an LG G5 with Marshmallow and I receive no GPS updates, i.e. onLocationChanged() is never called.
The permissions are granted (the App view on the phone's settings shows Location permission) and the following statement is true
checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
AndroidManifest.xml includes these lines:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
The code I am using is as below:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = getLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
private LocationListener getLocationListener() {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
handleLocationChange();
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
return locationListener;
}
I tried NETWORK_PROVIDER as well, no success.
What am I missing here?
I realized that the GPS Sensor is not providing any updates to the phone. Such as, when I restrict the Location precision to the GPS Sensor only, Google Maps never get a location fix.
If I connect the phone to the internet, then the NETWORK_PROVIDER provides location updates.

Android unable to get current location using GPS when device is not moving

I want to get current latitude and longitude of device even when it is not in movement.
Tried both requestLocationUpdates() and requestSingleUpdate() but my location is still null. LocationManager.onLocationChanged() seems to be called only when location gets changed. I want to post this location details but fails with NullPointerException every time.
Here are my code samples:
Activity:
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
if (location == null) {
try {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, myLocationListener, Looper.myLooper());
} catch (SecurityException e) {
}
}
//MyLocationListener
public void onLocationChanged(Location location) {
activity.setLocation(location);
}
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
If the user has not used GPS for a while, there will be no last known location, hence the GPS provider will not update at all unless you move outside. Its better to use the new FusedLocationApi to get updates as it selects the best source for you and gives you the best possible location.
https://developer.android.com/training/location/receive-location-updates.html
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Trying putting your code in AsyncTask.

Android Device Location from Ethernet

I've a TI OMAP platform running Kitkat 4.4.4. The platform doesn't have Wifi/GPS/Cellular connection but it has ethernet. I use eth1 to access internet.
Is there anyway to determine the coarse geographical location of the device based on eth1. I tried the sample code below, but it fails for ethernet connection.
/** Check if we can get our location */
public void checkLocation(){
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
try{
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d(TAG,"Latitude: " + location.getLatitude() + ", Longitude = " + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {Log.d(TAG,"location found 1");}
public void onProviderEnabled(String provider) {Log.d(TAG,"location found 2");}
public void onProviderDisabled(String provider) {Log.d(TAG,"location found 3");}
};
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
}catch(Exception e){
Log.e(TAG, "Location Exception: " + e.getMessage());
}
}
Above code throws provider doesn't exist: network exception for ethernet. Works OK on a device with wifi/GPS.
I've also set the following permissions in manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Any ideas how to determine location if the device has on ethernet connectivity?
Thanks
You can use
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
and
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
to check the availability of your network or gps. If these two return false to you, then you might not be able to use the locationManager to find location.

why my gps is not triggered?

i have a problem with my app getting my GPS position.
first of all i have the two things in my manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
then i have a code like this for the location:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(0l, 0f, new Criteria(), locationListener, Looper.getMainLooper());
now the strange thing:
when i have wifi enabled and i'm in my network, i get a position. If i have only GPS enabled i dont get my position. even i dont see the gps icon at the top of the phone which shows when a app makes use of my gps.
what is wrong here?
For GPS:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
For Network:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Categories

Resources