I'd like to check the user's assisted GPS setting in Android. When I change the 'location accuracy' setting, a ContentObserver tells me the URI is content://settings/global/assisted_gps_enabled. I don't see a constant for this in android.provider.Settings.Global; how do I access the value of this setting?
Try the following code to read that value:
int value = 0;
ContentResolver cr = getContentResolver();
try {
value = Settings.Global.getInt(cr,"assisted_gps_enabled");
} catch (Exception e) {
android.util.Log.e("get agps_enabled",e.toString());
}
results on Android 8.0, It returns 1 (Enabled by default)
To write that value:
Settings.Global.putInt(cr, "assisted_gps_enabled", your-value);
This permission <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> is required and this permission is only granted to system apps
A-GPS is turned on in phones when a user has set GPS settings to "Battery savings" and in order to use it in your app, you will have to add permission:
android.permission.ACCESS_COARSE_LOCATION
in your manifest xml file, if you are running app below android 6.0 (Marshmellow), else you will have to ask permissions at runtime,
see here:
https://developer.android.com/training/permissions/requesting
Now, how you will got to know programatically that user has using AGPS or GPS you can use LocationManager class for that and can pass either LocationManager.NETWORK_PROVIDER; for AGPS or LocationManager.GPS_PROVIDER for GPS respectively
for GPS provider, in android settings it is termed as "High accuracy" setting and for that permission required is android.permission.ACCESS_FINE_LOCATION
how to use it?
see here: https://developer.android.com/guide/topics/location/strategies#java
for further help, please have a look:
for runtime permissions: https://developer.android.com/training/permissions/requesting
for AGPS
https://en.wikipedia.org/wiki/Assisted_GPS
// 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.
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);
Related
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" />
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?
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.
In Android if LocationServices is disabled, is there any way that I can get the current location through wifi/network. I know there is one way of showing the user a popup to change his LocationSettings , but I don't want to show the popup.
So is there any way of getting the current location even if LocationServices is disabled ?
use LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER
The entire code and how to use is given here :
http://developer.android.com/guide/topics/location/strategies.html
// 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.
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);
Yes of course, you can use:
// let Android select the right location provider for you
String myProvider = locationManager.getBestProvider(myCriteria, true);
check this for more details good-way-of-getting-the-users-location-in-android and this Location Strategies
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.