How can i enable and disable NETWORK_PROVIDER programatically - android

We all know about that GPS Issues on Android.
How do we create the same turnOnGps Function Bellow to a NETWORK_PROVIDER, I want turn on Both.
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
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);
} }

Just adding onto what CommonsWare mentioned, that method only works on Android versions of 2.2 and below.
However, you can use the, simple, code below:
startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
This will take the user directly to the screen where they can enable/disable Location based options.
Refer here (docs), for more settings
Hope this helps

Fortunately, the script-kiddie technique you cite above does not work on modern versions of Android. You cannot enable or disable any location provider yourself, for blindingly obvious privacy reasons. Please allow the user to enable or disable location providers through the OS-supplied mechanisms (Settings, Power Control app widget, etc.).

Related

Android : Turning GPS on Programmatically [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enable GPS programatically like Tasker
All,
I seen most of the answers in this forum that we CANT turn on gps programmatic ally. I am wondering in some of the applications available in Android market, how do they do? I tried most of the things explained in this forum, it always gives a prompt to the user to enable the Location based services.
Any other thoughts?
Thanks,
Ramesh
Not sure what you mean by "turn it on". Do you mean enable or disable it? This thread discusses some ways to enable or disable it by exploiting bugs: How can I enable or disable the GPS programmatically on Android?. I wouldn't use this method though, since it won't work on devices where the bug has been fixed. Far better to forward the user to the settings app and let them enable gps.
Do you mean to turn it on or off to use it, assuming it's been enabled? You don't need to do that. Simply call LocationManager.requestLocationUpdates() with the right location provider, and the system will turn it on for you.
Have you looked here?
http://developer.android.com/guide/topics/location/strategies.html
What you can do is assert what type of location provider is enabled, check if it is GPS, if not add this to prompt the user to turn on GPS (be sure to run a long Toast to let the user know to enable GPS location and to hit the back button once enabled):
ComponentName intentComponent = new ComponentName("com.android.settings", "com.android.settings.LocationSettings");
Intent mainIntent = new Intent("android.intent.action.MAIN");
mainIntent.setComponent(intentComponent);
startActivity(mainIntent);

How to disable location service programmatically?

Is there any way to disable google location service programmatically?
I want to write an app to turn it off with a widget.
Thanks for your help!
Is there any way to disable google location service programmatically?
No, sorry, you cannot disable GPS, etc. programmatically, except via an app installed on the system partition or signed by the firmware's signing key. IOW, you need a rooted phone.
As far as i know, the LocationManager is a system service that can not be turned off. Though, you can disable all the location providers that the service uses, such as Wireless location, GPS, etc.
If you're using the UiAutomater, you can turn off Location Services by simulating a click on the Location option in the Quick Settings. The filter logic may be platform dependent. You may analyse the view hierarchy using the uiautomatorviewer.
i.e.
UiDevice.getInstance(getInstrumentation()).openQuickSettings();
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().descriptionContains("Location").className(ViewGroup.class)).click();
build.gradle
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
Short answer is, No. But if you really need to turn it off, you can prompt the user to do it:
LocationManager loc = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
if( loc.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
{
Toast.makeText( this, "Please turn off GPS", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
startActivity(myIntent);
}
else
{
// Do nothing
}
This might not help your situation but is the only way I know of changing the settings.

How to turn on and turn off (enable and disable) Location Services programatically in ICS?

I'd like to know if it possible to enable and disable Location Services programatically in Android 4.0?
I've found several approaches how to do this for previous versions of Android (for instance, this is the most popular link). But these approach does not work for Android ICS.
Also, I understand that an application should not do this but, for instance, default widget does this.
Can anybody clarify if it is possible and how to achieve this?
In a tiny app I have developed, I turn off the gps by just disabling the location on the MyLocationOverlay. Of course, this will work only if you are using maps from Google as that class belongs to the Google API's.
myLocationOverlay.disableMyLocation();
I place the line on the onStop() method and only if it is currently enabled. And every time my app goes to the background, the GPS turns automatically off.
Hope it helps.
You can enable and disable gps device but users must confirm in setting widget.
The solution in this topic.
LocationManager L = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!L.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent I = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(I);
}

Switch off GPS programmatically

What i have: Currently my app is giving location through gps.
what i want: Gps to turn off automatically after i exit from the application. Because it keeps on telling me the location time and again that looks odd and also gps consume a lot battery.
Looking at above comment thread it seems its possible to turn OFF GPS programatically (But seeing only 12 Upvotes)
But if you switch OFF GPS from your application programatically, what if other applications use GPS Service ?
The solution would be like
Open Settings of android and then tell user to turn OFF GPS when he exits from the application...
For this you can do like :
Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
OR
You can try like
locationManager.removeUpdates(myLocationListener);
locationManager = null;
This shutdown gps for this app, but its still available for use by other apps.
This code can alter your gps.But it is not documented
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);
Even though the link Rasel posted contains some code which might work you should keep in mind that the code is exploiting a security flaw which has been fixed already (http://code.google.com/p/android/issues/detail?id=7890) and therefore shouldn't work in the near future anymore.
If you want to enable the GPS programmaticaly then copy and paste this code in your project, its working fine.
final Intent i = new Intent();
i.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
i.addCategory(Intent.CATEGORY_ALTERNATIVE);
i.setData(Uri.parse("3"));
sendBroadcast(i); //if you are in broad cast receiver activity then use context.sendBroadcast(i)

How to enable GPS in android coding [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Enable GPS programatically like Tasker
Ok, I know there is same question asked before but all answer is "NO". It is proven that enable GPS can be done in coding. Don't believe? try out an application called "LOOKOUT". I believe there is some workaround in order to achieve this.
I had read through lookout AndroidManifest.xml, what i found out they only use
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
Anyone have idea how they managed to do this? what is the secret behind? I personally managed to do this only if the application is install as system app and also require two permission
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
My Code:
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
thank you.
According to this post on the Lookout support forums, they are unable to programmatically enable GPS.
If Lookout could enable the GPS receiver remotely, then use the service, then turn it off – we would, but unfortunately there is no “disable GPS for everything other than Lookout” and if you have disabled the GPS receiver, no application can make use of it. Enabling the GPS receiver requires a user to do it manually.
Edit: It looks like in the new version of Lookout they are most likely now exploiting a security flaw to accomplish this. It is not guaranteed to continue to work, but according to an open Android bug report from April 2010, code similar to the following will successfully programmatically enable/disable GPS (at least until the flaw is fixed):
private void toggleGPS(boolean enable) {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps") == enable) {
return; // the GPS is already in the requested state
}
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"));
context.sendBroadcast(poke);
}
Also, see the question "Enable GPS programmatically like Tasker" and the XDA thread mentioned there for more discussion on how people have used this method.
If you do use this method, you should make it clear to potential users of your application, since this is a HUGE potential privacy concern.

Categories

Resources