2013-02-05 21 views
1

我必須提交我的迷你項目。我寫了一個類來獲取用戶位置。我想知道我的方法中是否有任何技術問題。它工作正常。我正在使用處理程序來連續獲取位置。我的位置方法有什麼問題嗎?

Main.java

public class Main extends Activity { 

LocationTracker lt; 
TextView tv; 
Handler handler = new Handler(); 
Runnable locationRunner = new Runnable() { 

    @Override 
    public void run() { 
     if (lt.canGetLocation()) { 
      tv.setText(lt.getLatitude() + " " + lt.getLongitude()); 
     } 
     handler.postDelayed(this, 1000); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lt = new LocationTracker(this); 
    Toast.makeText(
      this, 
      "GPS: " + lt.isGPSEnabled() + "\nNetwork: " 
        + lt.isNetworkEnabled() + "\nCanGetLocation: " 
        + lt.canGetLocation(), Toast.LENGTH_LONG).show(); 
    tv = (TextView) findViewById(R.id.textView1); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    handler.postDelayed(locationRunner, 1000); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    handler.removeCallbacks(locationRunner); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

LocationTracker.java

public class LocationTracker implements LocationListener { 

private Context context; 
private boolean isGPSEnabled; 
private boolean isNetworkEnabled; 
private boolean isTracking; 
private Location location; 
private LocationManager locationManager; 

public LocationTracker(Context context) { 
    this.context = context; 
    locationManager = (LocationManager) context 
      .getSystemService(context.LOCATION_SERVICE); 
    isNetworkEnabled = locationManager 
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    isGPSEnabled = locationManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 
    if (!isNetworkEnabled && !isGPSEnabled) { 
     isTracking = false; 
    } else { 
     isTracking = true; 
    } 

    if (isGPSEnabled) { 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, this); 
    } else if (isNetworkEnabled) { 
     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    } 

} 

public double getLatitude() { 
    if (location != null) { 
     return location.getLatitude(); 
    } else { 
     return 0.00; 
    } 
} 

public double getLongitude() { 
    if (location != null) { 
     return location.getLongitude(); 
    } else 
     return 0.00; 
} 

public boolean canGetLocation() { 
    return isTracking; 
} 

public boolean isGPSEnabled() { 
    return isGPSEnabled; 
} 

public boolean isNetworkEnabled() { 
    return isNetworkEnabled; 
} 

@Override 
public void onLocationChanged(Location arg0) { 
    location = arg0; 
} 

@Override 
public void onProviderDisabled(String arg0) { 
} 

@Override 
public void onProviderEnabled(String arg0) { 
} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 

} 

} 

`

+0

顯示我們yewr Logcat – jenuine

回答

1

是啊,在我看來,兩件事情是錯誤的。

  1. 不檢查requestLocationUpdates的可用性。
  2. 取而代之的是監聽DATA連接更改,並且removeUpdatesrequestLocationUpdates

示例代碼

// OnCreate中

tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    tm.listen(new NetworkConnectionState(this.getApplicationContext()), 
      PhoneStateListener.LISTEN_DATA_CONNECTION_STATE 
| PhoneStateListener.LISTEN_CELL_LOCATION); 

//你的聽衆

public void onDataConnectionStateChanged(int state, int networkType) { 
     // We have changed protocols, for example we have gone from HSDPA to 
     // GPRS 
     // HSDPA is an example of a 3G connection 
     // GPRS is an example of a 2G connection 
     if (state == TelephonyManager.DATA_CONNECTED) { 
     } 
     else { 
     } 
    } 

編輯: 不要忘記在onProviderDisabledonProviderEnabled更新您的連接/斷開你的位置說唱歌手

+0

赦免先生,但爲什麼我不必檢查可用性?我正在偵聽requestLocationUpdates()。你能解釋一下嗎?我沒有得到你:-) –

+0

你的意思是onDestroy()調用,我停止偵聽updateListener?好吧,我知道了 –

+0

你只是一次檢查可用性,而不是這個網絡的事情是非常不穩定的,你需要知道什麼時候網絡可用,什麼時候不可用。你希望系統發送一個事件/回調給你做一些事情。這是一個更好的設計。 – Siddharth

0

如果在應用啓動時gps和網絡都被禁用,則isTraking = false,即使用戶在應用運行期間啓用gps或網絡,它也不會更改爲true。您應該使用onProviderDisabled和onProviderEnabled方法中的邏輯。

+0

在我得到位置之前,我正在檢查一個'boolean'變量,只有在兩個提供程序中的一個被啓用的情況下才會變爲true。如果(lt.canGetLocation()){ tv.setText(lt.getLatitude()+「」+ lt.getLongitude());}這個** 'public void run() } handler.postDelayed(this,1000); }' 但是在我的應用程序運行期間,我可以根據情況操縱此標誌,無論是真或假 –

+0

o是的。我明白了@Jason Yong。謝謝。對我好 :-) –