Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable GPS in android coding [duplicate]

Tags:

android

gps

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.

like image 466
Jason Avatar asked Mar 30 '11 03:03

Jason


1 Answers

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.

like image 192
Brian Avatar answered Oct 25 '22 03:10

Brian