Android 网络提供商不提供位置更新
这是我注册网络提供商的代码Android 网络提供商不提供位置更新,android,Android,这是我注册网络提供商的代码 netlocationListener = new MynetLocationListener(); locationMangaer.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,interval,mttravel, netlocationListener); /*----------Listener
netlocationListener = new MynetLocationListener();
locationMangaer.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,interval,mttravel,
netlocationListener);
/*----------Listener class to get coordinates ------------- */
private class MynetLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Toast.makeText(getBaseContext(), "accuracy & network provider"+loc.getAccuracy(),Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "location changed", Toast.LENGTH_LONG).show();
CharSequence time1=android.text.format.DateFormat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());
.LENGTH_LONG).show();
String date =time1.toString();
float tsp= 0;
Toast.makeText(getBaseContext(),"Location changed : Lat: " + loc.getLatitude()
+ " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
String longitude = Double.toString(loc.getLongitude());
Log.v(TAG, longitude);
String latitude =Double.toString( loc.getLatitude());
Log.v(TAG, latitude);
/*----------to get City-Name from coordinates ------------- */
String cityName=null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (addresses.size() > 0)
// System.out.println(addresses.get(0).getLocality());
cityName=addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
String accuracy=Float.toString( loc.getAccuracy());
String s = longitude+"\n"+latitude +"\n\nMy Currrent City is: "+cityName + "accuracy"
+ accuracy + "speed"+loc.getSpeed();
}
若我将提供商更改为GPS,那个么它工作得很好,但我需要从网络提供商那个里获取位置。我检查了互联网和wifi的工作情况,但仍然没有结果
它甚至不需要输入onlocationchanged()。对于基于网络的定位,它们比GPS精确得多,这意味着您需要将设备移动更大的距离,以允许信号在之前连接到另一个网络塔
调用onLocationChanged()。也不是所有的网络塔都提供位置。我看不出您在哪里初始化位置管理器。我猜你不会使用网络位置收到消息,因为你移动的距离不够,无法触发位置更改。网络位置不是很准确。我看到网络定位距离超过100英尺,可能会有人猜测你的设备有100英尺的“误差半径”,你必须移动到外面才能获得位置更新 我已经在下面剪贴了我的代码,你知道里程数可能会有所不同
// see if this line gives you what you want
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// get location via network
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// get location via GPS
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
您是否已将网络提供商更改为GPS提供商?是的,它将起作用。我已检查过。我认为您错过了添加网络状态的权限。我已经添加了两个:)我正在使用GPS,所以我不认为这是一个问题。因此,仅当我移动大约4公里时,它可能会调用onLocationChanged()是吗?但它必须至少第一次调用onLocationChanged(),对吗?这取决于它是否能够首先获得位置更新。如果它甚至不能得到一个更新,比如在飞机模式下,onLocationChanged将永远不会被调用。@likeitlikeit编辑是什么?让我知道你编辑了什么以及为什么?我想这可能是问题所在,所以接受你的回答谢谢你的回复,因为我的htc explorer显示n/w提供商未启用,但我的sony xperia tipo和三星young mobiles显示n/w提供商已启用,但没有显示位置。我已初始化LocationManager,但我没有显示,只显示主代码的想法。现在isproviderenabled()返回false,尽管我的internet已启动
// see if this line gives you what you want
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// get location via network
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// get location via GPS
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);