如何使用Android中的代码启动和停止GPS

如何使用Android中的代码启动和停止GPS,android,android-service,android-location,Android,Android Service,Android Location,我希望在应用程序启动时(oncreate()方法调用时)启用GPS 30秒。30秒后,它转为禁用模式 试试这个代码 LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, WLConstan

我希望在应用程序启动时(oncreate()方法调用时)启用GPS 30秒。30秒后,它转为禁用模式

试试这个代码

 LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
 locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, WLConstants.DELAY_HOUR, gpsl
                        .getMinDistance(), gpsl);
要停止GPS,请尝试

locationManager.removeUpdates(gpsl);
完成你的时间段后再给我这个

try this code

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);      
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER,
            1,
            0,
            myLocationListener
    );