我试图计算安卓应用程序的速度,每当它被改变。我得到了速度,但我有两个问题:
1-考虑第一次变化并计算速度需要时间。
2-速度不准确,当我开车时,手机给出的速度与汽车的速度不一样!
我的代码是:这个在onCreate方法中:mlocManager.requestLocationUpdates (LocationManager。GPS_PROVIDER, 20000,100年,mlocListener);
我使用了LocationListener接口,我实现了这个方法:
public void onLocationChanged(Location loc){
if (firsttime) {
la1 = loc.getLatitude();
lo1 = loc.getLongitude();
firsttime = false;
start = System.currentTimeMillis();
} else {
la2 = loc.getLatitude();
lo2 = loc.getLongitude();
end = System.currentTimeMillis();
long difftime = end - start;
double diffhoure = (double) difftime / (1000 * 60 * 60);
double dis = distance(la1, lo1, la2, lo2);
double speed = dis / diffhoure;
Toast.makeText(getApplicationContext(),
"Your speed is" + speed, Toast.LENGTH_SHORT).show();
la1 = la2;
lo1 = lo2;
start = end;
}
}
计算距离的方法为:
double distance(double lat1, double lon1, double lat2, double lon2) {
// this method uses Haversine Formula
double R = 6373; // earth radius in KM.
double dlon = Math.toRadians(lon2 - lon1);
double dlat = Math.toRadians(lat2 - lat1);
double a = (Math.sin(dlat / 2)) * (Math.sin(dlat / 2))
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * (Math.sin(dlon / 2))
* (Math.sin(dlon / 2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c;
return d;
}
另外,你能告诉我一个计算WiFi和3G带宽的好方法吗?
提前谢谢你。
感谢你的帮助。
您可以使用此代码获得wifi带宽:
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
WifiManager mgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo networks = mgr.getConnectionInfo();
if (networks != null) {
Integer linkSpeed = networks.getLinkSpeed();
Log.i("linkSpeed**************",linkSpeed +"");
}
}