How to enable GPS in android coding [duplicate]

我们两清 提交于 2019-12-17 17:54:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5481695/how-to-enable-gps-in-android-coding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!