2017-05-24 1 views
-1

Ich versuche, kontinuierliche Standortaktualisierungen in meiner App mit Fused Location Provider zu bekommen, irgendwie funktionierte es gut in Lollipop, aber in Marshmallow zeigt es keine Standortaktualisierung.Wie man Fused Location Provider in Android Api Level 23 implementieren

Unten ist mein code.Plz lassen Sie mich wissen, was ich falsch bin.

public class Gps extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

TextView lati, longi; 
LocationRequest lr; 
GoogleApiClient mGoogleApiClient; 
double latitude = 0, longitude = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gps); 

    lati = (TextView) findViewById(R.id.lati); 
    longi = (TextView) findViewById(R.id.longi); 

    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 


} 

@Override 
protected void onStart() { 
    super.onStart(); 

    mGoogleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

    if (mGoogleApiClient.isConnected()) { 

     mGoogleApiClient.disconnect(); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    lr = LocationRequest.create(); 
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    lr.setInterval(1000); 

    if (Build.VERSION.SDK_INT >= 23) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      requestPermissions(new String[]{ 

        Manifest.permission.ACCESS_FINE_LOCATION 
      }, 1); 

      return; 
     } 

    } 
    else{ 

     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, lr,this); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 

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

    Geocoder gc = new Geocoder(Gps.this, Locale.getDefault()); 

    String locality = null; 
    String sub = null; 

    try { 

     List<Address> addrList; 

     addrList = gc.getFromLocation(latitude, longitude, 2); 

     if (addrList.size() > 0) { 

      Address adr = addrList.get(0); 
      locality = adr.getLocality(); 
      sub = adr.getSubLocality(); 

      lati.setText(locality); 
      longi.setText(sub); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    switch (requestCode) { 

     case 2: 
      if(){ 
      }     
      else{ 

        } 
    } 

} 

}

Antwort

0

Sie haben keine Logik für Standortaktualisierungen in Ihrer onRequestPermissionsResult Funktion anzufordern. Sie fordern auch nur ACCESS_FINE_LOCATION und nicht ACCESS_COARSE_LOCATION an, obwohl Sie nach beiden suchen.

Edit:

In Ihrer onConnected Funktion, die Sie nie LocationServices.FusedLocationApi.requestLocationUpdates() anrufen, wenn Sie auf Eibisch oder darüber. Im Wesentlichen geht es dir nur dies:

if (Build.VERSION.SDK_INT >= 23) { 
    if (/*No permissions*/) { 
     //Request permissions 
     return; 
    } 
    //Location updates are never requested here!!! 
} 
else { 
    //Request location updates 
} 

Wann sollte es so etwas wie sein: für das Erklären jetzt

if (Build.VERSION.SDK_INT >= 23) { 
    if (/*No permissions*/) { 
     //Request permissions 
     return; 
    } 
} 
//Request location updates 
+0

Dank i Anfrage auch für Coarse Standort hinzugefügt, aber wie Standortaktualisierungen in onRequestPermissionsResult beantragen PLZ mich, . – Digvijay

+0

Rufen Sie 'LocationServices.FusedLocationApi.requestLocationUpdates()' in dieser Funktion auf. Andernfalls wird es in API 23+ nie aufgerufen. –

+0

Ich versuche, LocationServices.FusedLocationApi.requestLocationUpdates hinzuzufügen (mGo ogleApiClient, lr, this); in OnRequestPermissionResult, aber zeigt Fehler beim Hinzufügen der Berechtigungsprüfung, aber ich habe die Berechtigung bereits überprüft. – Digvijay

Verwandte Themen