2011-08-16 44 views
0

我仍在測試android gps功能,現在我的Eclipse現在提供一個位置onCreate()getLastKnownLocation()和一個onLocationChanged()事件。但是當我通過telnet-interface的geo fix命令發送進一步的位置更改時,我不再收到任何事件調用。這可能是什麼原因?Android GPS只是第一個位置更改onLocationChanged的結果,爲什麼?

我的輸出意味着他在啓動應用程序(有一個GpsStatus.GPS_EVENT_STARTED事件)後開始偵聽更改,然後在接收位置更新後自動停止偵聽(有一個GpsStatus.GPS_EVENT_STOPPED事件)。

詳細詢問,並看看我的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LocationManager l =(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    List<String> li = l.getAllProviders(); 
    for (Iterator<String> iterator = li.iterator(); iterator.hasNext();) { 
      String string = iterator.next(); 
      Log.i(TAG, string); 
    } 
    if (l.getLastKnownLocation("gps")==null) 
     Log.i(TAG, "null"); 
    else{    
     Log.i(TAG, l.getLastKnownLocation("gps").toString()); 
    } 

    l.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
} 

... 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    String txt = "onStatusChanged:"+arg0+":"; 
    switch(arg1){ 
     case GpsStatus.GPS_EVENT_FIRST_FIX: 
      txt += "first fix"; 
      break; 
     case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
      txt += "sat status"; 
      break; 
     case GpsStatus.GPS_EVENT_STARTED: 
      txt += "event started"; 
      break; 
     case GpsStatus.GPS_EVENT_STOPPED: 
      txt += "event stopped"; 
    } 
    Log.i(TAG, txt); 
} 

... 

@Override 
public void onLocationChanged(Location arg0) { 
    Log.i(TAG, "onLocationChanged:" +arg0.toString()); 
} 

這裏的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.mypackage.example.gps" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="3" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".gpsActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

回答

1

放入的onResume方法您的位置更新代碼..

這樣,

@Override 
    public void onResume() { 
     super.onResume(); 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,1.0f, this); 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 1000L, 1.0f, this); 
    locationManager.addGpsStatusListener(this); 

} 

並且還給出參數,如毫秒中的時間和浮點中的距離。

希望這會幫助你。

如果您發現該回答有用,請爲其他人投票。

+0

感謝您幫我清理一下我的代碼。但它沒有解決或解決問題。順便說一句。無論你是否要求他們這樣做,人們都會贊成有用的迴應。 – erikbwork

+1

你能告訴我你的清單文件中給出的gps訪問權限類型嗎? – user370305

+0

當然,我在上面的問題中添加了清單內容。 – erikbwork

相關問題