2013-04-20 37 views
2

我正在開發一個gps程序,我每5分鐘獲得一次gps值, 它工作的很好,但我必須存儲我得到的值。它每5分鐘刷新一次,並且我只有一個文本視圖,以便在刷新新值時刪除舊值。如何從文本框android中存儲值?

這是我的代碼。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", true); 
     this.sendBroadcast(intent); 

     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(!provider.contains("gps")){ //if gps is disabled 
      final Intent poke = new Intent(); 
      poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
      poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
      poke.setData(Uri.parse("3")); 
      this.sendBroadcast(poke); 


     } 
    { 
     //initialize location manager 
     manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     //check if GPS is enabled 
     //if not, notify user with a toast 
     if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)); else { 
      //get a location provider from location manager 
      //empty criteria searches through all providers and returns the best one 
      String providerName = manager.getBestProvider(new Criteria(), true); 
      Location location = manager.getLastKnownLocation(providerName); 

      TextView tv = (TextView)findViewById(R.id.locationResults); 
      if (location != null) { 
       tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
      } else { 
       tv.setText("Last known location not found. Waiting for updated location..."); 
      } 
      //sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute 
      manager.requestLocationUpdates(providerName, 60000, 1, this); 
     } 
    } 
} 

    @Override 
    public void onLocationChanged(Location location) { 
     TextView tv = (TextView)findViewById(R.id.locationResults); 
     if (location != null) { 
      tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
     } else { 
      tv.setText("Problem getting location"); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String arg0) {} 

    @Override 
    public void onProviderEnabled(String arg0) {} 

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

    // Find the closest Bart Station 
    public String findClosestBart(Location loc) { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 

     double curStatLat = 0; 
     double curStatLon = 0; 
     double shortestDistSoFar = Double.POSITIVE_INFINITY; 
     double curDist; 
     String curStat = null; 
     String closestStat = null; 

     //sort through all the stations 
     // write some sort of for loop using the API. 

     curDist = Math.sqrt(((lat - curStatLat) * (lat - curStatLat)) + 
         ((lon - curStatLon) * (lon - curStatLon))); 
     if (curDist < shortestDistSoFar) { 
      closestStat = curStat; 
     } 

     return closestStat; 

     } 

謝謝。

+0

商店它在一個ArrayList中 – Raghunandan 2013-04-20 05:03:20

+0

追加新值該textview。 – 2013-04-20 05:04:05

+0

你能告訴我如何顯示列表中的位置,我得到。 – 2013-04-20 05:07:26

回答

1

您可以將Textview的值存儲到文件中以存儲持久性。研究我的答案正確,我增加了文件存儲方法,現有的代碼,

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", true); 
     this.sendBroadcast(intent); 

     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(!provider.contains("gps")){ //if gps is disabled 
      final Intent poke = new Intent(); 
      poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
      poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
      poke.setData(Uri.parse("3")); 
      this.sendBroadcast(poke); 


     } 
    { 
     //initialize location manager 
     manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     //check if GPS is enabled 
     //if not, notify user with a toast 
     if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)); else { 
      //get a location provider from location manager 
      //empty criteria searches through all providers and returns the best one 
      String providerName = manager.getBestProvider(new Criteria(), true); 
      Location location = manager.getLastKnownLocation(providerName); 

      TextView tv = (TextView)findViewById(R.id.locationResults); 
      if (location != null) { 
       tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
      } else { 
       tv.setText("Last known location not found. Waiting for updated location..."); 
      } 
      //sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute 
      manager.requestLocationUpdates(providerName, 60000, 1, this); 
     } 
    } 
} 

    @Override 
    public void onLocationChanged(Location location) { 
     TextView tv = (TextView)findViewById(R.id.locationResults); 
     if (location != null) { 
      tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
      // I have added this line 
      appendData (location.getLatitude() + " latitude, " + location.getLongitude() + " longitude"); 
     } else { 
      tv.setText("Problem getting location"); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String arg0) {} 

    @Override 
    public void onProviderEnabled(String arg0) {} 

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

    // Find the closest Bart Station 
    public String findClosestBart(Location loc) { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 

     double curStatLat = 0; 
     double curStatLon = 0; 
     double shortestDistSoFar = Double.POSITIVE_INFINITY; 
     double curDist; 
     String curStat = null; 
     String closestStat = null; 

     //sort through all the stations 
     // write some sort of for loop using the API. 

     curDist = Math.sqrt(((lat - curStatLat) * (lat - curStatLat)) + 
         ((lon - curStatLon) * (lon - curStatLon))); 
     if (curDist < shortestDistSoFar) { 
      closestStat = curStat; 
     } 

     return closestStat; 

     } 
    // method to write in file 
public void appendData(String text) 
{  
    File dataFile = new File("sdcard/gpsData.txt"); 
    if (!dataFile.exists()) 
    { 
     try 
     { 
     dataFile.createNewFile(); 
     } 
     catch (IOException e) 
     { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 
    try 
    { 
     //BufferedWriter for performance, true to set append to file flag 
     BufferedWriter buf = new BufferedWriter(new FileWriter(dataFile, true)); 
     buf.append(text); 
     buf.newLine(); 
     buf.close(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

你需要寫下列權限在AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

@Lucifer我是新來的機器人,我有很多樣品和它的服役中運行,但我不知道編輯,你可以告訴我如何請 – 2013-04-20 05:34:38

0

那麼你有足夠的持久性選項,但在這種情況下,最好是使用SharedPreferences

+0

有一點疑問,你如何在SharedPreference中存儲多個具有相同鍵值的經度值? – Lucifer 2013-04-20 05:34:45

0

沒有人可以肯定地說沒有確切知道你需要保存的數據做什麼。如果您需要臨時存儲它,ArrayList是一個不錯的選擇。您可以創建一個新的ArrayList,然後在setText()中使用它的同時將該值存入該處。如果你想要永久的東西,那麼你可能想要將它存儲在數據庫或文件中。退房Storage Options

而且,在這種情況下,一個好主意,可將其存儲在一個ArrayList暫時然後使用該名單將他們轉移到一個文件或數據庫永久保存,如果這就是你想要

另一個什麼方式臨時存儲它,並可能保存在某個地方以後將是一個HashMap。可能是HashMap<String, HashMap<String, String>>的形式。由於我們不知道數據的準確意圖,所以例子可能是無止境的,但也許這會給你一個很好的起點,所以你可以決定什麼最適合你,那麼你可以找到很多例子,遍及SO和Google for您的選擇

相關問題