mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { msg = msg + "\n位置(onLocationChanged): " + location.getLongitude() + "," + location.getLatitude(); } else { msg = msg + "\n位置(onLocationChanged): location is null"; } new SendMsgTask().execute(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { Location location = mLocationManager.getLastKnownLocation(provider); if (location != null) { msg = msg + "\n位置(onProviderEnabled): " + location.getLongitude() + "," + location.getLatitude(); } else { msg = msg + "\n位置(onProviderEnabled): location is null"; } new SendMsgTask().execute(); } @Override public void onProviderDisabled(String provider) { } });
这个位置的监听,即使页面跳转到其他页面,依然会保持监听,除非退出app
在监听的过程中,消息栏一直会提示程序在使用定位功能
不晓得为什么每次获取的位置都是null,即使加了监听也还是null
provider = LocationManager.NETWORK_PROVIDER; location = mLocationManager.getLastKnownLocation(provider); if (location != null) { msg = msg + "\n位置(" + provider + "): " + location.getLongitude() + "," + location.getLatitude(); } else { msg = msg + "\n位置(" + provider + "): location is null"; }
使用network的方式倒是成功过两次