2013-06-11 46 views
2

我试图在Android应用程序发生更改时计算速度。我得到了速度,但我有两个问题:在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?

预先感谢您。

我感谢您的帮助。

回答

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 +""); 

     } 
} 
+0

对我来说非常有用。 – 22332112