【发布时间】:2011-11-23 06:50:46
【问题描述】:
我有一个使用 Network_Provider 和 onLocationChanged() 实现 LocationListener 的应用程序,我将新位置记录在带有时间戳的日志文件中。此应用程序作为服务运行。
我看到这个异常行为:
它似乎只从 wi-fi 位置捕获位置(没有蜂窝网络提供商)
一旦服务启动,它会一直等待,直到找到第一个 wi-fi 位置。在那之前它不会记录任何东西。
一旦通过 wi-fi 找到位置,它就会开始记录位置纬度/经度
此后此日志记录会继续,每 45 秒一次,即使位置未更改..
此后此记录会继续,每 45 秒一次,即使设备已移出该 wi-fi 的范围,事实上,从我家到工作地点 @ 20 英里(工作时没有 wi-fi)并且它仍然继续每 45 秒记录一次相同的位置。
奇怪?
这是我的代码:
public class TestLocationService extends Service implements LocationListener {
LocationManager lm;
public TestLocationService() {
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String loc= "Time : "+new Date()+"\n";
loc += "New Location "+location.getLatitude()+" and "+location.getLongitude()+"\n*******************\n";
System.out.println(loc);
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/loc");
dir.mkdirs();
File file = new File(dir, "loc.txt");
FileOutputStream fOut = new FileOutputStream(file,true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(loc);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onCreate() {
this.lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
-abhay
【问题讨论】:
-
谢谢@frayab!将我的代码添加到上面的主要问题中。
-
对不起,我的回答迟了。关于第 1 点:您只想从 wi-fi 位置捕获位置吗?或者您的愿望是与最佳供应商合作?关于第 2 点:如果您只告诉网络提供商是逻辑,它会等到找到第一个 wi-fi 位置。关于第4点:我认为这是Android中位置监听器的正常行为。关于第5点:如果您想在提供者超出范围时停止接收坐标,您必须填写 onProviderDisabled 方法并调用 lm.removeUpdates(this);
-
我想使用最好的提供商 .. 但问题是,即使使用 NETWORK_PROVIDER,为什么手机网络提供商例如AT&T 或 T-Mobile 不提供位置更新。它只接收 Wi-Fi 位置更新。由于我始终连接到手机网络,因此我应该始终拥有当前位置。
-
如果您使用 network_provider,您将只会收到 WIFI 位置更新。
-
我需要做什么才能从手机服务提供商(例如美国的 AT&T 和印度的 IDEA)接收位置更新?
标签: android networking provider locationlistener