首页 > 【Android】使用模拟位置,关闭模拟位置后GPS启动不了

【Android】使用模拟位置,关闭模拟位置后GPS启动不了

问题描述:

应用中开启了模拟位置,当应用关闭或者在应用中手动关闭模拟位置后,手机的GPS无法启动,必须重启手机才能恢复正常。

代码详情:
LocationManager的初始设置(应用本身也需要开启GPS)

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
locationManager.addGpsStatusListener(gpsStatusListener);
locationManager.addNmeaListener(nmeaListener);

开启模拟位置,覆盖掉GPS时的代码如下(通过点击事件开启的):

mMockProviderName = LocationManager.GPS_PROVIDER;
locationManager.setTestProviderEnabled(mMockProviderName, true);
locationManager.requestLocationUpdates(mMockProviderName, 1000, 0, locationListener);

点击关闭模拟位置时的代码如下:

locationManager.setTestProviderEnabled(mMockProviderName, false);                        
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
locationManager.removeTestProvider(mMockProviderName);

程序退出时关闭模拟位置的代码如下

locationManager.setTestProviderEnabled(mMockProviderName, false);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
locationManager.removeTestProvider(mMockProviderName);
locationManager.removeUpdates(locationListener); locationManager.removeGpsStatusListener(gpsStatusListener);      
locationManager.removeNmeaListener(nmeaListener);

基本上都是按照官方文档来的,该现象在google上搜索了一番,并没有发现类似问题的解决办法。想请教一下各位出现如问题所述的现象是什么原因,如何修改。

【热门文章】
【热门文章】