2016-02-03 223 views
2

在我的android应用程序中,我每3分钟检索一次用户位置,并以某种方式按照以下方式执行此操作。到目前为止,我遇到过的问题有:如何从GPS或网络提供商获取位置android

  1. 每当我收到位置更新时,我在同一时间获得多个值
  2. 有时候我一次同时获得GPS和NETWORK提供者值。

我只需要一个值,它也应该是GPS或NETWORK Provider的值,如何实现这一点。

这里是我的服务

public class MyService extends Service { 
private static final String TAG = "TESTGPS"; 
private LocationManager mLocationManager = null; 
private static final int LOCATION_INTERVAL = 180000; 
private static final float LOCATION_DISTANCE = 0.0f; 
double lati,longi; 
String loc_name; 

private class LocationListener implements android.location.LocationListener { 
    Location mLastLocation; 


    public LocationListener(String provider) { 
     Log.i(TAG, "LocationListener " + provider); 
     mLastLocation = new Location(provider); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.i(TAG, "onLocationChanged: " + location); 
     Toast.makeText(getApplicationContext(), ""+location.getLatitude()+"==>"+location.getLongitude(), Toast.LENGTH_SHORT).show(); 

     lati = location.getLatitude(); 
     longi = location.getLongitude(); 

     Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
     try { 
      List<Address> addressList = geocoder.getFromLocation(
        lati, longi, 1); 
      if (addressList != null && addressList.size() > 0) { 
       { 
        loc_name = addressList.get(0).getAddressLine(1); 

       } 
       AsyncHttpClient client = new AsyncHttpClient(); 
       final RequestParams params = new RequestParams(); 
       params.put("sendingJSON", composeLocation()); 
       Toast.makeText(getApplicationContext(), "" + params, Toast.LENGTH_LONG).show(); 
       client.post("http://192.168.0.120/gpstracker/send_location.php", params, new AsyncHttpResponseHandler() { 

        public void onSuccess(String response) { 

         Log.i("Status ==> ", "Sent to server"); 

        } 

        public void onFailure(int statusCode, Throwable error, String content) { 
         if (statusCode == 404) { 

          Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show(); 
         } else if (statusCode == 500) { 

          Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show(); 
         } else { 

          Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show(); 
         } 
        } 

       }); 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 

      mLastLocation.set(location); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Log.i(TAG, "onProviderDisabled: " + provider); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Log.i(TAG, "onProviderEnabled: " + provider); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     Log.i(TAG, "onStatusChanged: " + provider); 
    } 
} 

LocationListener[] mLocationListeners = new LocationListener[]{ 
     new LocationListener(LocationManager.GPS_PROVIDER), 
     new LocationListener(LocationManager.NETWORK_PROVIDER) 
}; 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

public int onStartCommand(Intent intent, int flags, int startId) { 
    //Toast.makeText(MyService.this, "Service Started", Toast.LENGTH_SHORT).show(); 

    Log.i("Service Started", "Started"); 
    return START_STICKY; 

} 

@Override 
public void onCreate() { 
    Log.e(TAG, "onCreate"); 
    initializeLocationManager(); 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[1]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
    } 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[0]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
    } 
} 

@Override 
public void onDestroy() { 
    Log.e(TAG, "onDestroy"); 
    super.onDestroy(); 
    if (mLocationManager != null) { 
     for (int i = 0; i < mLocationListeners.length; i++) { 
      try { 
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider calling 
        // ActivityCompat#requestPermissions 
        // here to request the missing permissions, and then overriding 
        // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
        //           int[] grantResults) 
        // to handle the case where the user grants the permission. See the documentation 
        // for ActivityCompat#requestPermissions for more details. 
        return; 
       } 
       mLocationManager.removeUpdates(mLocationListeners[i]); 
      } catch (Exception ex) { 
       Log.i(TAG, "fail to remove location listners, ignore", ex); 
      } 
     } 
    } 
} 
private void initializeLocationManager() { 
    Log.i(TAG, "initializeLocationManager"); 
    if (mLocationManager == null) { 
     mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

    } 
} 

public String composeLocation() 
{ 
    ArrayList<HashMap<String, String>> locationList = new ArrayList(); 
    HashMap<String, String> map = new HashMap(); 
    String parsed_lati = String.valueOf(lati); 
    String parsed_longi = String.valueOf(longi); 
    map.put("e1", "123"); 
    map.put("lati",parsed_lati); 
    map.put("longi",parsed_longi); 
    map.put("loc_name",loc_name); 
    locationList.add(map); 
    return new GsonBuilder().create().toJson(locationList); 
} 


} 

回答

5

你应该得到两个位置之前先检查。首先检查位置是否可以通过GPS,如果没有,然后使用网络获取位置。 这是一个示例类,它有检查位置的方法。

public class GPSTracker extends Service implements LocationListener { 

    private final Context context; 

    boolean isGPSEnabled = false; 
    boolean isNetworkEnabled = false; 
    public boolean canGetLocation = false; 

    Location location; 

    double latitude; 
    double longitude; 

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 

    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.context = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 

      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if(!isGPSEnabled && !isNetworkEnabled) { 

      } else { 
       this.canGetLocation = true; 

       if (isNetworkEnabled) { 

        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

         if (location != null) { 

          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 

       } 

       if(isGPSEnabled) { 
        if(location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         if(locationManager != null) { 
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

          if(location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 


    public void stopUsingGPS() { 
     if(locationManager != null) { 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

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

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

     return longitude; 
    } 

    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 

     alertDialog.setTitle("GPS is settings"); 

     alertDialog.setMessage("Turn on your GPS to find nearby helpers"); 

     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       context.startActivity(intent); 
      } 
     }); 

     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 

     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location arg0) { 
     // TODO Auto-generated method stub 

    } 

    @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 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 
+0

这个类只是为了解哪个提供者会给位置权? – ShivLeela

+0

是的,它也有方法来显示警报,如果你只有GPS作为获取位置的选项,它会显示提醒用户打开GPS –

+0

@VivekMishra真的不错的代码;有没有机会看到你如何在一项活动中使用这项服务?非常感谢。 – Isabelle

1

您应该使用Criteria请求位置更新,而不是使用提供者:

Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    locationManager.requestSingleUpdate(criteria, listener, null); 

这样,你可以只使用一个监听器和位置值可以从GPS或网络提供商。

相关问题