Android 解决 GetLastKnownLocation(provider) = null

事情的起因就是我用系统的GetLastKnownLocation输出结果永远是null。后来看大家说这个方法在室内90%都不管用。

于是查了国内各大网站给出了两个解决办法:

1.while(GetLastKnownLocation(provider) = null){

    GetLastKnownLocation = null

}

2.locationManager.setTestProviderEnabled(provider1, false);

然而这两种方法并没能解决我的问题。只是疯狂在while里循环,知道我的App崩溃。。。

最后查询StackOverflow得到解决办法。

用如下办法获取Last Location

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();

private Location getLastKnownLocation() {
    mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}

结果很成功。

激动之余记录一下,并分享给大家。

原文链接:https://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在Android设备上获取用户的当前位置并获取城市,可以使用Android系统提供的LocationManager类和Geocoder类。 首先,需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> ``` 然后,在代码中使用LocationManager类获取设备的当前位置: ```java LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); ``` 这里使用NETWORK_PROVIDER来获取网络位置,也可以使用GPS_PROVIDER来获取GPS位置。获取到位置后,就可以使用Geocoder类将位置转换成城市: ```java Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); String city = addresses.get(0).getLocality(); ``` 这里需要注意的是,getFromLocation方法需要在异步线程中执行,以避免阻塞UI线程。完整的代码示例如下: ```java public class MainActivity extends AppCompatActivity { private TextView cityTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cityTextView = findViewById(R.id.city_text_view); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { new GetCityTask().execute(location); } } private class GetCityTask extends AsyncTask<Location, Void, String> { @Override protected String doInBackground(Location... locations) { Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(locations[0].getLatitude(), locations[0].getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); } if (addresses != null && addresses.size() > 0) { return addresses.get(0).getLocality(); } else { return null; } } @Override protected void onPostExecute(String city) { if (city != null) { cityTextView.setText(city); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值