2014-01-18 46 views
1

我是一名android初學者,我需要您的幫助!我正在構建一個非常簡單的運行應用程序,並希望收集有關速度的信息。爲此,我正在嘗試創建一個非常簡單的GPS類,以便使用getspeed()方法。Android:在模擬器上工作的GPS座標,但不在電話上

我創建了一個小測試應用程序,以確保我的代碼是健全的。在這個應用程序中,我也使用getLatitude()和getLongitude(),但這些對我的最終代碼沒有用 - 我只需要速度。

到目前爲止,我已經成功地在模擬器上測試了這個類,但不是在手機上。使用DDMS座標時,我可以看到屏幕上顯示的座標(getspeed()保持爲0,但我知道我不能在模擬器上測試)。當我嘗試手機上的代碼時,我完全沒有任何反應 - 儘管等待GPS「暖身」(當我移至GoogleMaps時,我也看到他的GPS工作正常)。

整件事情讓我瘋狂,我不確定onLocationChanged()方法發生了什麼,所以感謝您給我提供的任何幫助。 謝謝!

的代碼是按照下文:

import android.app.Activity; 
    import android.content.Context; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class MainActivity extends Activity { 
    TextView speedText1, speedText2, speedText3; 
double speed = 0; 
double latitude = 0; 
double longitude = 0; 
String speed1; 
String latitude1; 
String longitude1; 
boolean gps_enabled; 

Context context; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 

      Toast.makeText(getBaseContext(), "location not null" , Toast.LENGTH_SHORT).show(); 


      speedText1 = (TextView) findViewById(R.id.textView1); 
      speedText2 = (TextView) findViewById(R.id.textView2); 
      speedText3 = (TextView) findViewById(R.id.textView3); 

      speed = location.getSpeed(); 
      speed1 = Double.toString(speed); 
      speedText1.setText(speed1); 

      latitude = location.getLatitude(); 
      latitude1 = Double.toString(latitude); 
      speedText2.setText(latitude1); 

      longitude = location.getLongitude(); 
      longitude1 = Double.toString(longitude); 
      speedText3.setText(longitude1); 

      Toast.makeText(getBaseContext(), latitude1 + longitude1 + speed , Toast.LENGTH_SHORT).show(); 



     }  

     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     public void onProviderEnabled(String provider) { 

     } 

     public void onProviderDisabled(String provider) { 

     } }; 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    } 
} 
+0

的可能重複(http://stackoverflow.com/questions/4811920/why-getspeed-always [爲什麼getSpeed()總是在Android返回0] -return-0-on-android) – FWeigl

+0

不,請知道getSpeed()不能在模擬器中工作。我只是想知道如果有人知道爲什麼這個代碼不會在手機上工作。 – user2943342

回答

2

您所定義的onCreate()方法裏面的LocationListener的,所以你不會得到位置更新,一旦方法完成。你應該嘗試這樣的事:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements LocationListener { 

TextView speedText1, speedText2, speedText3; 
double speed = 0; 
double latitude = 0; 
double longitude = 0; 
String speed1; 
String latitude1; 
String longitude1; 
boolean gps_enabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0.0f, this); 
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 


    Toast.makeText(this, "location not null" , Toast.LENGTH_SHORT).show(); 


    speedText1 = (TextView) findViewById(R.id.textView1); 
    speedText2 = (TextView) findViewById(R.id.textView2); 
    speedText3 = (TextView) findViewById(R.id.textView3); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     speed = location.getSpeed(); 
     speed1 = Double.toString(speed); 
     speedText1.setText(speed1); 

     latitude = location.getLatitude(); 
     latitude1 = Double.toString(latitude); 
     speedText2.setText(latitude1); 

     longitude = location.getLongitude(); 
     longitude1 = Double.toString(longitude); 
     speedText3.setText(longitude1); 

     Toast.makeText(this, latitude1 + longitude1 + speed, Toast.LENGTH_SHORT).show();    

    } 

} 

}

+0

你是明星!代碼按預期工作。謝謝巴勃羅! – user2943342

相關問題