2013-09-25 22 views
0

我想從Android讀取GPS數據。我已經寫了一些代碼遵循在線教程閱讀Android GPS時沒有反應

public class MainActivity extends Activity 
{ 

private DataCollection mDataCollection; 

private LocationManager mLocationManager; 
private Location mLocationGPS; 
private String strLocationProvider = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Config.mContext = this; 
    mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
    mLocationGPS = getLocationProvider(mLocationManager); 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
} 

protected void onResume() 
{ 
    super.onResume(); 
    // mDataCollection.register(); 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
} 

protected void onPause() 
{ 
    super.onPause(); 
    // mDataCollection.unregister(); 
    mLocationManager.removeUpdates(locationListener); 
} 

public void startCollection(View view) 
{ 
    // mDataCollection.register(); 
} 

public void stopCollection(View view) 
{ 
    // mDataCollection.unregister(); 
} 

private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     if (location != null) 
     { 
      double longitude = location.getLongitude(); 
     } 
    } 

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

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

    private Location getLocationProvider(LocationManager lm){ 
     Location retLocation = null ; 

     Criteria mCriteria01 = new Criteria(); 
     mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); 
     mCriteria01.setAltitudeRequired(false); 
     mCriteria01.setBearingRequired(false); 
     mCriteria01.setCostAllowed(true); 
     mCriteria01.setPowerRequirement(Criteria.POWER_HIGH); 

     strLocationProvider = lm.getBestProvider(mCriteria01, true); 
     retLocation = lm.getLastKnownLocation(strLocationProvider); 

     return retLocation; 
    } 
} 

當我把一個破發點,在double longitude = location.getLongitude();,在這一點斷點永遠不會停止。我也有包括清單許可

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.collectdata" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

有人可以幫助我。在此先感謝

+0

你怎麼知道location.getLongitude沒有響應。你用日誌檢查位置?也許有斷點的麻煩。 – edisonthk

+0

如何檢查日誌。我在location.getLongitude()中放置了一個斷點。當我讀取傳感器數據時,斷點工作 – user2753594

回答

2

您在測試時至少移動了10米嗎?這就是「10」的含義mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

另外,在onCreate()onResume()中都不需要該行。從你的onCreate()方法中刪除它(儘管它不應該導致問題,只是更加整潔)。

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

+0

當我將「10」更改爲「0」時,也沒有迴應 – user2753594

+0

我發現location = getLastKnownLocation(LocationManager.GPS_PROVIDER);該位置仍然爲空,但我使用網絡它的工作原理。是因爲我不是站在外面嗎? – user2753594

+0

是的,我想這是因爲GPS尚未鎖定。這可能需要幾分鐘... – nomachinez