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

时间:2011-06-20 12:10:17

标签: android android-service android-location

我想在我的应用程序启动时启用GPS 30秒(当oncreate()方法调用时)。 30秒后转为禁用模式。

2 个答案:

答案 0 :(得分:8)

试试这段代码,

 LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
 locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, WLConstants.DELAY_HOUR, gpsl
                        .getMinDistance(), gpsl);

并停止GPS尝试

locationManager.removeUpdates(gpsl);

在你完成你的时间后给出这个。

答案 1 :(得分:0)

try this code

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