Turn on GPS in Jellybean - android

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

Related

How to Switch ON and OFF GPS

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

requestSingleUpdate doesn't work :(

I have the following code:
SosServiceListener mlistener = new SosServiceListener(this);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// getting network status
boolean isNetworkEnabled = manager.
isProviderEnabled(LocationManager.NETWORK_PROVIDER);
String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
manager.requestSingleUpdate(provider, mlistener, null);
manager.getLastKnownLocation(provider);
but the SosServiceListener is never called :( any idea why?
String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
is GPS enabled on the device, with Satellite option set in Security section of mobile settings? because you are assuming that GPS_PROVIDER is enabled, just because NETWORK_PROVIDER is disabled? which is not accurate ...
also manager.getLastKnownLocation(provider); will return Location object directly and will not result a call to callback methods in the listenet...
so you need to use either one of them...

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:

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.

criteria for both GPS and network

I would like to know how can I get location from the best provider
do I have to make two separate criteria 1 for the GPS and 1 for the network or is there a way to put them all together ?
this is my code when I add the COARSE criteria the GPS does not go on (no GPS flashing logo on the top of the screen) and when I use the FINE criteria I dont get any thing from the network.......so do I have to write criteria for both and switch between them for what ever is available or can they both be in the same criteria ?
because I have the "getBestProvider(criteria, true);" in my code so it should get the location from the best provider....right..??!
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
Using a getBestProvider with Criteria.ACCURACY_FINE will never return the NETWORK_PROVIDER, even though wifi position is usually quite accurate.
In my application, I use getProviders to get all providers matching my criteria, then add the NETWORK_PROVIDER if it is activated and not yet in the list.
Then I launch a requestLocationUpdates for each of these providers, making sure to call removeUpdates when I get a location accurate enough.
This way, if the location provided by the network is accurate enough, the gps provider will be turned off
#nicopico getBestProvider with Criteria.ACCURACY_FINE can return the NETWORK_PROVIDER if gps is not enabled
i use the following code in my app
public void getLocation() {
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
Log.e(TAG, "getLocation fired Google Play Services are not available");
mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
context.getResources().getString(R.string.gpserv_notfound)));
}
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
Log.e(TAG, "network location provider not enabled");
} else {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Check provider exists then request for location
if (provider != null) {
requestLocation(locationManager, provider);
} else {
//start wifi, gps, or tell user to do so
}
}
}
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");
if (provider != null) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
//locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
//tell user what has happened
}
}
this code is in a class that implements locationListener
you can edit this line
locationManager.requestLocationUpdates(provider, 0, 0, this);
to reflect your implementation of locationListener

Categories

Resources