start Android gps service

后端 未结 2 741
逝去的感伤
逝去的感伤 2021-02-04 18:07

is there any way to turn on gps service through code?

2条回答
  •  遇见更好的自我
    2021-02-04 19:00

    There's a good example on HelloAndroid

    Check for the GPS

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
    boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
    

    If isGPS is false open the settings

    startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
    

    Don't forget to check the GPS is enabled when the user returns, they might not enable it.

提交回复
热议问题