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 被禁用,位置将无法进入三星手机
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01