2013-05-10 45 views
0

我有如下代碼設置當前位置的應用程序:Android的固定位置應用

public void setLocation(double latitude, double longitude) { 

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (locationManager.getProvider(providerName) != null) { 
      locationManager.removeTestProvider(providerName); 
     } 
     locationManager.addTestProvider(providerName, true, false, false, 
       false, false, false, false, Criteria.POWER_LOW, 
       Criteria.ACCURACY_FINE); 

     Location myloc = new Location(LocationManager.GPS_PROVIDER); 
     myloc.setLatitude(latitude); 
     myloc.setLongitude(longitude); 
     myloc.setTime(System.currentTimeMillis()); 
     locationManager.setTestProviderEnabled(providerName, true); 
     locationManager.setTestProviderLocation(providerName, myloc); 
    } 

    @SuppressWarnings("deprecation") 
    public void set1001(View view) { 
     final double latitude = 40.718803; 
     final double longitude = -74.000193; 
     setLocation(latitude, longitude); 
     displayLocation(); 
} 

set1001是Button點擊動作。

問題是,在設置位置後,它將切換回當前位置。這個設置位置代碼是否必須不斷運行,就像每秒鐘左右一樣?基本上我希望位置固定在我指定的座標上。

感謝您的任何幫助。

+0

你想一次設置座標,並使用他們很多次都沒有改變 – 2013-05-10 21:20:58

+0

的例子是的,這是excatly我想做! – 2013-05-10 21:25:04

回答

0

你希望做任務可以使用sharedpreferences

因爲你正在做的 - 首先得到座標的值來實現。

- 現在將這些值存儲在shared preferences中。

-使用存儲在共享首選項中的值而不是運行代碼來獲取座標。

- 存儲在共享首選項中的值只有在您更改或刪除後纔會更改。

你可以得到如何將值設置爲從鏈路共享偏好低於 http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

http://developer.android.com/reference/android/content/SharedPreferences.html

+0

我希望其他應用程序在我的應用程序運行後獲得相同的座標位置,所以基本上我僞裝了我當前的GPS位置。 – 2013-05-10 21:35:57