How to Switch ON and OFF GPS - android

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.)

Related

Android LocationManager network provider returns null

I wanted to get my GPS coordinates using Android App. I started developing, and I can get GPS coordinates, but they are not accurate. I wanted to use NETWORK_PROVIDER, but the Location by this provider is always null. More interesting, isProvicerEnabled returns true.
I used example from this thread (best answer)
enter link description here
private void _getLocation() {
// Get the location manager
try {
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
double latitude = -1;
double longitude = -1;
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
if (isNetworkEnabled) {
showToast("network");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
1000,
0, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
else if (isGPSEnabled) {
showToast("gps");
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
0, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
showToast("" + latitude + longitude);
} catch (Exception e) {
e.printStackTrace();
}
I have all the permissions in manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
I know the code is dirty, but for now it's only for testing. Do I miss something? I found similar examples in many places, and it seems pretty straight, so I am a little confused.
My phone works ok, GPS and network works fine. For example Google Maps application I have works well. Any suggestions?
Please do NOT use this code. It's bad. It has a lot of errors. Also, getLastKnownLocation will return null if it doesn't have a location yet. Which it always will if nobody on the phone is using requestUpdates.
Your code is taken from a class that was posted on a very old thread on here called GPSTracker. I've been trying to kill that code for months- it causes far more problems than it helps. If you want better example code, try http://gabesechansoftware.com/location-tracking/ which is a blog post I wrote about how bad that code is. It will show you the correct way to do it, and explains some of what's wrong with that code.

Turn on GPS in Jellybean

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);
}

Can't get location on Samsung Galaxy Tab 2 & Note

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:

Location is null on device

I am testing my app on a real device and getting a null location when the GPS is on. When I test on the emulator with dummy coordinates it works fine. What is wrong?
locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
listener = new MyLocationListener();
viaGps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
viaNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!viaGps && !viaNetwork) {
tracking = false;
} else {
if (viaGps) {
Log.d("", "gps is on");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else if (viaNetwork) {
Log.d("", "network is on");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location == null) {
Log.d("", "location not fouond");
}
Try using this , it solved my problem
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Also check your permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
You need a Location listener:
Follow the documentation
Update:
You can also try that easy way: What is the simplest and most robust way to get the user's current location in Android?

Gps is not working after restart my mobile

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.

Categories

Resources