i am developing an application , in that i need to find current location values. For that, i enabled gps programmatically. now, i switched off and switched on my mobile. Now, i unable to get current location values but my device's gps is turned on.
i found this problem in (motorola 2.3.3 ,htc 2.3.4, and nexus 4.2.1)
i used the following code.
String provider = Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
List<String> provider = locationManager.getProviders(true);
Location location = null;
for (int i=provider.size()-1; i>=0; i--)
{
location = locationManager.getLastKnownLocation(provider.get(i));
if (location != null) break;
if (location != null)
{
String latitude=String.valueOf(location.getLatitude());
String longitude=String.valueOf(location.getLongitude());
}
after turning on GPS you must call requestLocationUpdate so that GPS starts sensing.
Hope This may help.
Related
This question has been asked many a times and i have found many results but none of them really helped me.
Using the below code to Switch ON GPS
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.i("GPS IS ON", "GPS IS ON"+provider.contains("gps"));
if(!provider.contains("gps")){ //if gps is disabled
Log.i("GPS IS ON", "GPS IS ON");
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
and to switch OFF :
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
i am calling these 2 different methods on 2 different clicks.It should automatically switch on the GPS on the device but its not working.
i have even added these permissions :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I have tested these in 4.4 and 4.3 and does on Switch on the GPS automatically.
Also LOCATION_PROVIDERS_ALLOWED is Deprecated in 4.4 so what is the alternative for that ?
EDIT
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
try {
gps_enabled = locManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
// if(!gps_enabled && !network_enabled)
// return false;
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locListener);
}
if (gps_enabled) {
location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (network_enabled && location == null) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locListener);
}
if (location != null) {
MyLat = location.getLatitude();
MyLong = location.getLongitude();
}
I have had this code working but seems that if internet is Not there it wont work with GPS turned OFF.
I think one cannot change the user's personal phone settings(Like GPS turn on-off, sound on-off,display etc.) from any application. It would be security voilation.
In my opinion here only option is to check if GPS is on or off and Redirect User to Settings screen.
LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
showPopupToRedirectToSettings();
}
By using following Intent you can launch settings screen:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)
I am trying to retrieve Location as follows:
LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// NOTE: the location below is null !!
Location location = locationManager.getLastKnownLocation(provider);
String lat = location.getLatitude();
String lng = location.getLongitude();
}
The 'location' above is null. Why?
But when i open the (Google) Maps app - it shows location correctly, and even a notification icon (looks like a exclamation mark) shows up.
And there is a GPS Coordinates app - which also shows up blank.
I have turned on Settings > 'Location' on the phone. If it matters this is Android 4.4.4 Sony Experia phone.
Well, it seems that you're not checking properly if the GPS is working or not. The way you should do it:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//GPS is not enabled !!
}
You can also create an AlertDialog so the user is aware that the GPS is not enabled and you can send him to the GPS settings by doing this:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
After this, I'd implement a location listener and get coordinates, here is an example, check it out
How do I get the current GPS location programmatically in Android?
UPDATE: They might be getting coordinates through other provider (usually the one which is working better at that moment). So if the GPS it not working, just try other provider, here is an example:
Location lastKnownLocation = null;
List<String> providers = null;
if(locationManager != null) providers = locationManager.getAllProviders();
if(providers != null)
{
for(int i=0; i<providers.size(); i++)
{
if(locationManager != null) lastKnownLocation = locationManager.getLastKnownLocation(providers.get(i));
if(lastKnownLocation != null)
{
position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
break;
}
}
}
I know this question has been asked a million times. Unfortunately I am still clueless about it. I am also developing an app which tells me to turn on GPS and turn it off. I have tried various suggestions such as these:
1.)
private void turnGpsOn (Context context) {
String beforeEnable = Settings.Secure.getString (context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
String newSet = String.format ("%s,%s",
beforeEnable,
LocationManager.GPS_PROVIDER);
try {
Settings.Secure.putString (context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
newSet);
} catch(Exception e) {}
}
2.)
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Unfortunately none of these suggestions work . I also had WRITE_SECURE_SETTINGS in the AndroidManifest.xml. Still doesnt work. Is there any solution to this problem or is it possible at all to turn on GPS.
Thanks,
Uday
Just Refer my below code
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
I use this code to get the location:
final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
final Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
final String providerName = locationManager.getBestProvider(locationCritera, true);
if (providerName != null && locationManager.isProviderEnabled(providerName)) {
locationManager.requestLocationUpdates(providerName, 20000, 100, this);
} else {
Toast.makeText(this, "GPS turned off!", Toast.LENGTH_LONG).show();
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions are defined in manifest.
Both network & GPS are turned on, but I'm getting only onProviderEnabled called with param "network" (once). But the same code works fine on 4.0 emulator & Onda 4.1 Tab.
Why?
I have a GPS example. Maybe you can use it as reference:
Example is here:
In my application I want to get the location of the user and show it on a map (google maps).
In the settings of a android device you can check
Use Wireless networks
Use GPS satellites
When the user starts the mapactivity I show a dialog in wich the user can choose to get his current location or not.
Old code deleted
If the user wants to get his location I check if the gps is enabled. If not, I start the settings. Else I try to get the location. But even when GPS is active and "use wireless networks" not he won't get a location. Is there a solution to check if "Use wireless networks" is enabled. Or what part of my code I need to change to get location only by GPS signal?
Thanks
EDIT mycurrentcode:
boolean isGps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetwork = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGps && !isNetwork){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if(isGps && !isNetwork) {
String bestProvider = LocationManager.GPS_PROVIDER;
Location location = lm.getLastKnownLocation(bestProvider);
Toast.makeText(MapsTabActivitiy.this, "Location niet beschikbaar.", Toast.LENGTH_SHORT).show();
} else if(!isGps && isNetwork) {
String bestProvider = LocationManager.NETWORK_PROVIDER;
Location location = lm.getLastKnownLocation(bestProvider);
if(location == null){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (location != null){
mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
mapcontroller.setZoom(15);
}
}
else {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bestProvider);
if(location == null){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (location != null){
mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
mapcontroller.setZoom(15);
}
}
So I check with isProviderEnabled for GPS_PROVIDER AND NETWORK_PROVIDER. And with the if, else if I change how to get the location. The only case I need to find is case 2 (else if(isGps && !isNetwork){}). Get the location by GPS signal.
You're getting the right provider using the Criteria class, but you can simple use the LocationManager.GPS_PROVIDER to request the location updates using only GPS. Change
String bestProvider = lm.getBestProvider(criteria, false);
to
String bestProvider = LocationManager.GPS_PROVIDER;
Hope this helps.