Location is not getting in samsung phone if GPS is disabled(如果 GPS 被禁用,位置将无法进入三星手机)
问题描述
我使用三星手机通过 LocationManager API 获取位置,如果禁用 GPS,我无法获取位置,通过网络提供商我无法获取位置.
I use samsung phone to get location through LocationManager API, i'm unable to get location if GPS is disabled, through network provider i'm unable to get the location.
这是代码,这在 HTC &索尼甚至禁用了 GPS,但三星手机没有.
Here is the code, this works well in HTC & sony even disabling GPS but not in Samsung phone.
public Location getLocation() {
try {
mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); //fails in samsung phone returns NULL
if (isGPSEnabled == false && isNetworkEnabled == false) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
推荐答案
这种行为是三星手机独有的.HTC你不会遇到这个问题.
This behavior is unique to Samsung Phones. HTC you wont face this issue.
您必须启动 locationManager,因为默认情况下它会查看 google maps 缓存并且不关心缓存对于当前位置信息的陈旧程度.执行此操作,启动您的应用程序,形成不同的 gps 位置并注意它仍然显示旧位置.现在启动谷歌地图,然后重新启动你的应用程序,你的位置就会改变.要以编程方式启动您的应用程序以获取当前位置",请在调用 getLastKnownLocation
You have to kick start locationManager, since by default it looks into the google maps cache and does not care about how stale that cache is for the current location information. Do this, launch your application, form a different gps location and notice that it still shows the old location. Now launch google maps, and relaunch your app, your location would have changed. To programmatically kick start your app for "current location" run the following code before you call getLastKnownLocation
YOUR_APPLICATION_CONTEXT.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
这篇关于如果 GPS 被禁用,位置将无法进入三星手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果 GPS 被禁用,位置将无法进入三星手机


- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01