2

我试图在 Android 应用程序更改时计算它的速度。我得到了速度,但我有两个问题:

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带宽的好方法吗?

先感谢您。

我感谢您的帮助。

4

1 回答 1

1

您可以使用以下代码获取 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 +"");

        }
}
于 2014-04-02T16:15:37.093 回答