如何根据Android应用程序中的条件启用和禁用GPS更新(LocationManager.requestLocationUpdates(...))

时间:2016-10-17 11:10:50

标签: android gps

我正在开发一个应用程序,我想根据条件启用和禁用GPS更新。在启动应用程序时,一切正常,即使onLocationChanged()回调被调用,但如果我启动应用程序然后打开GPS,则永远不会调用LocationListener回调。我正在使用IntentService来拨打GPS服务课程。我是Android的新手,请帮忙。

//Intent service code
public class MyLocationService extends IntentService {

    protected int runningLocationScan;

    GPSTracker GPSobjLM;

    public MyLocationService() {
        super("myLocationService-thread");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("SERVICE", "ON HANDLE INTENT");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        GPSobjLM = new GPSTracker(this);

        if(GPSobjLM.isGPSEnabled)
            runningLocationScan = 1;
        else
            runningLocationScan = 0;

        Log.d("SERVICE", "ONCREATE");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(GPSobjLM.isGPSEnabled)
            GPSobjLM.stopUsingGPS();
    }

    private void startGPSUpdates(){
        if(runningLocationScan==0)
             GPSobjLM.getGPSLocationUpdates();
        if(GPSobjLM.isGPSEnabled)
            runningLocationScan = 1;
    }

    public void stopGPSUpdates(){
        if(runningLocationScan!=0)
            GPSobjLM.stopUsingGPS();
            runningLocationScan = 0;
    }


thread-----
   if(condition) {
      startGPSUpdates();
      runningLocationScan = 0;
   }
   else {
      stopGPSUpdates();
      runningLocationScan = 1
   };


//GPSTracker service 

public class GPSTracker extends Service {

    private static Context mContext;

    // flag for GPS status
    boolean  isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    public static Location mylocation;
    double latitude; 
    double longitude; 

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meter 
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 1; // 1 seconds 

    // Declaring a Location Manager
    protected LocationManager locationManager;
    LocationListener locationListener;

    public GPSTracker(Context context) {
        mContext = context;
        getGPSLocationUpdates();
    }

    public void getGPSLocationUpdates() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {

            } else {
                if (isGPSEnabled) {
                    if (mylocation == null) {
                         locationListener = new LocationListener() {

                             @Override
                            public void onLocationChanged(Location location) { }

                             @Override
                            public void onStatusChanged(String provider, int status, Bundle extras) { }

                            @Override
                            public void onProviderEnabled(String provider) { }

                             @Override
                            public void onProviderDisabled(String provider) { }
                          };

                        if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
                            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Location getLocation() {
        if(mylocation != null)
           return mylocation;
           else return null;
    }

    public void stopUsingGPS() {
        if(locationListener != null) {
            if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
            }
            locationManager.removeUpdates(locationListener);
        }       
    }

    public double getLatitude() {
        if(mylocation != null)
            latitude = mylocation.getLatitude();
        return latitude;
    }

    public double getLongitude(){
        if(mylocation != null)
            longitude = mylocation.getLongitude();
        return longitude;
    }

1 个答案:

答案 0 :(得分:0)

根据您的代码,您将获得每秒/每米的位置更新

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meter 
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 1; // 1 seconds 

但您的onLocationChanged为空

   @Override
                        public void onLocationChanged(Location location) {


                        }

如果您在内部进行测试,有时onLocationChanged不会被调用。这很有趣,但你需要选择你的设备并走在路上..