2016-06-14 9 views
0

So erhalten Sie Standortaktualisierungsdaten vom Fused location provider , wenn GPS ausgeschaltet ist und Wifi eingeschaltet und verbunden ist.Verwenden von FusedLocation Provider für Android-Gerät

Ich kann Standortdaten erhalten, wenn GPS im Gerät eingeschaltet ist.

+0

Ich empfehle Dienste Google Standort lösen: https://developer.android.com/training /location/index.html –

Antwort

1

versuchen, diese

public class LocationService extends Service { 
    private LocationManager locationManager; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, flags, startId); 
     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       getLocation(); 
      } 
     }); 
     return Service.START_STICKY; 
    } 

    final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      //your code hear 
     } 

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

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

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

    private void getLocation() { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 

     if (locationManager != null) { 
      String provider = locationManager.getBestProvider(criteria, true); 
      if (provider != null) { 
       Location location = locationManager.getLastKnownLocation(provider); 
       updateWithNewLocation(location); 
       locationManager.requestLocationUpdates(provider, 500, 50, locationListener); 
      } else { 
       if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener); 
       } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener); 
       } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) { 
        locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener); 
       } 
      } 

     } 
    } 

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

    public void onDestroy() { 
     super.onDestroy(); 
     if (locationManager != null && locationListener != null) { 
      locationManager.removeUpdates(locationListener); 
     } 
    } 
} 
Verwandte Themen