【问题标题】:How to Clear Latitude and Longitude Value in Android, When Application is Closed关闭应用程序时如何在 Android 中清除纬度和经度值
【发布时间】:2012-04-02 13:10:20
【问题描述】:

对不起,我的英语不好。

就像我问的那样。我有这个类来处理位置变化。

package com.mousepad.runningkids.util;

import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MyLocationManager {

    private Context context;
    private double myLat = 0.0;
    private double myLon = 0.0;
    private LocationManager locationManager = null;
    private Location location = null;
    private Criteria criteria;
    private String locationName = null;
    private MyLocationListener mll;

    public MyLocationManager(Context ctx) {
        this.context = ctx;
    }

    private String setCriteria() {
        this.criteria = new Criteria();
        this.criteria.setAccuracy(Criteria.ACCURACY_FINE);
        this.criteria.setAltitudeRequired(false);
        this.criteria.setBearingRequired(false);
        this.criteria.setCostAllowed(true);
        this.criteria.setPowerRequirement(Criteria.POWER_LOW);
        return locationManager.getBestProvider(criteria, true);
    }

    public double getMyLatitude() {
        return this.myLat;
    }

    public double getMyLongitude() {
        return this.myLon;
    }

    public String getLocation() {
        return this.locationName;
    }

    public void onLocationUpdate() {
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        String provider = setCriteria();

        location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
        mll = new MyLocationListener();
        locationManager.requestLocationUpdates(provider, 1000, 0, mll);

    }

    public void stopLocationListenerService() {
        locationManager.removeUpdates(this.mll);
    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            this.myLat = location.getLatitude();
            this.myLon = location.getLongitude();
            // Toast.makeText(this.context,
            // "Lokasi Anda:\n" + this.myLat + "\n" + this.myLon,
            // Toast.LENGTH_LONG).show();
            getLocationName(this.myLat, this.myLon);
        } else {
            Toast.makeText(this.context, "Lokasi Anda Tidak Diketahui",
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void getLocationName(double lat, double lon) {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> adresses = geocoder.getFromLocation(lat, lon, 1);
            StringBuilder sb = new StringBuilder();
            if (adresses.size() > 0) {
                Address address = adresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    sb.append(address.getAddressLine(i)).append("\n");
                sb.append(address.getCountryName()).append("\n");
            }
            this.locationName = sb.toString();
            // Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location newLocation) {
            // TODO Auto-generated method stub
            myLat = newLocation.getLatitude();
            myLon = newLocation.getLongitude();
            getLocationName(myLat, myLon);
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub
        }

    }

}

当这个应用程序关闭时,最后的纬度和经度仍然被保存,当我再次运行它时,手机中保存的最后一个位置是最后一个位置,直到它监听并更新它的位置。

我的问题是如何在不关闭手机的情况下清除纬度和经度等位置(空间)数据。它是否保存在应用程序缓存中?因为我已经创建了一个代码来在应用程序关闭时清除缓存。

谢谢。

【问题讨论】:

    标签: android location listener


    【解决方案1】:

    您需要在 MyLocationManager 中添加如下所示的两个方法,这将在您调用 Activity 类的 onPause 方法时清除您的 lat 和 long 值。

      public  setMyLatitude(double paramLat) {
           this.myLat=paramLat;
      }
    
    public  setMyLongitude(double paramLong) {
         this.myLon=paramLong;
    }
    

    现在当你的应用程序关闭时调用这个 set 方法,这意味着

     @Override
      protected void onDestroy() {
        // TODO Auto-generated method stub
     //Call Set Method from Here ,set them to initial Value     
          super.onDestroy();
    }
    

    【讨论】:

      【解决方案2】:

      您可以在 onResume() 中启动位置管理器,并可以在 onPause() 中删除位置监听器,因此下次开始活动时,您将获得当前位置,因为 onResume() 它将调用 onLocationChanged(),您将获得新的位置或者你可以做的是 onResume() 使用预定义的地理点。或者其他方式是这样的。

      @Override
          protected void onResume() {
              // TODO Auto-generated method stub
              super.onResume();
              if (checkGPSservice() /*this is just of my use you dont need if block just write following line*/) {
                  locationManager.requestLocationUpdates(
                          LocationManager.NETWORK_PROVIDER, 0, 0, this);
              }
          }
      
          @Override
          protected void onPause() {
              // TODO Auto-generated method stub
              super.onPause();
              locationManager.removeUpdates(this);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        • 2012-06-30
        • 2021-02-12
        相关资源
        最近更新 更多