How can i enable and disable GPS programatically

2020-07-23 08:50发布

I am developing an application wherein I want to enable GPS, when I start my activity and I want to disable GPS when I exit my application. Can I do it through my program..

Any help would be appreciated.

标签: android gps
8条回答
来,给爷笑一个
2楼-- · 2020-07-23 09:31

You can start the GPS by using

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new CTLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);

To stop the gps provider use

locationManager.removeUpdates(locationListener);
查看更多
再贱就再见
3楼-- · 2020-07-23 09:39

I know it is too late but directly starting location service will not work. Instead use below code to start location service.

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
查看更多
登录 后发表回答