how to enable gps in android

限于喜欢 提交于 2019-12-01 01:17:43
Dany Y

You can't enable GPS for a user, what you can do is, if GPS s disabled, prompt the user with a message to ask him to enable GPS and send him to the settings to enable it

with this code :

Intent gpsOptionsIntent = new Intent(  
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
startActivity(gpsOptionsIntent);

This is a more detailed code on how to do it

http://java-blog.kbsbng.com/2012/08/displaying-dialog-in-android-to-prompt.html

To get the location you do this

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  /*try to see if the location stored on android is close enough for you,and avoid rerequesting the location*/
    Location location =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null
            || location.getTime()
                    - Calendar.getInstance().getTimeInMillis() > MAX_LAST_LOCATION_TIME)
    {

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, TIME_INTERVAL, LOCATION_INTERVAL, yourLocationListener);

    }
    else
    {
        //do your handling if the last known location stored on android is enouh for you
    }

in yourLocationListener, you implement what to do when you get the location

It's inappropiate to enable GPS from the code. You can't fo that.

There used to be an exploit that allowed the GPS to be turned on by an app with no special permissions. That exploit no longer exists as of 2.3 (in most ROMs).

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