2

私は5分ごとにgps値を取得しているという点でgpsプログラムを開発していますが、うまく機能していますが、取得した値を保存する必要があります。5分ごとに更新され、テキストビューが1つしかないため、新しい値が更新されると古い値が削除されます。

これは私のコードです。

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;

        }   

ありがとうございました。

4

3 に答える 3

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" />
于 2013-04-20T05:18:05.353 に答える
0

保存されたデータで何をする必要があるかを正確に知らなければ、誰も確実に言うことはできません. ArrayListは、一時的に保存する必要がある場合に適したオプションです。新しい ArrayList を作成し、 で使用すると同時にそこに値を入れることができますsetText()。永続的なものが必要な場合は、おそらく DB またはファイルに保存する必要があります。ストレージ オプションを確認する

また、この場合、それをArrayList一時的に保存し、そのリストを使用してファイルまたは DB に転送し、永続的に保存することをお勧めします。

一時的に保存し、後でどこかに保存する別の方法は、HashMapです。の形の何かかもしれませんHashMap<String, HashMap<String, String>>。データの正確な意図がわからないため、例は無限にある可能性がありますが、おそらくこれは良い出発点になるので、何が最適かを判断でき、SO や Google で多くの例を見つけることができます。あなたの選択

于 2013-04-20T05:09:20.687 に答える
0

永続化オプションはたくさんありますが、この場合はSharedPreferencesを使用するのが最善です

于 2013-04-20T05:04:48.877 に答える