1

Update 1: Ich habe herausgefunden, dass der Code tatsächlich funktioniert, aber es fragt nicht nach Erlaubnis, Ihren Standort zu verwenden. Wenn Sie die App-Berechtigungen aufrufen und manuell auf den Standort zugreifen, funktioniert dies. Ich weiß immer noch nicht, wie ich es erhalten kann, um nach Berechtigungen für den Standort zur Laufzeit zu fragen.Fused Location Provider funktioniert nur auf einigen Telefonen

Ich lerne über fusionierten Standortanbieter und machte eine App nur lat und lang anzuzeigen. Ich benutze mein LG G4 mit 6.0 und folgte diesem großartigen YouTube Tutorial, aber wenn ich es auf meinem Handy laufen lasse, zeigt es nur den Breitengrad und den langen Text an, nicht die GPS-Koordinaten. Ich habe es auf einem Motorolla X 2015 und Moto Droid Turbo getestet und es funktioniert.

Kompilierung SDK Version: 7.0
Gradle Version: 2.14.1
Android Plugin Version: 2.2.0
Build-Tools Version: 24.0.2

Manifest:

<?xml version = "1.0" encoding = "utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.nathan.gpstest"> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 

       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

Gradle Dependencies:

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:24.2.1' 
    testCompile 'junit:junit:4.12' 
    //compile 'com.google.android.gms:play-services:9.6.1' 
    compile 'com.google.android.gms:play-services-location:9.6.1' 
} 

MainActivity:

package com.example.nathan.gpstest; 

import android.content.pm.PackageManager; 
import android.location.Location; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.FusedLocationProviderApi; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

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

    TextView latitudeText; 
    TextView longitudeText; 
    private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi; 
    private GoogleApiClient googleApiClient; 
    private LocationRequest locationRequest; 
    private double myLatitude; 
    private double mylongitude; 

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

    latitudeText = (TextView) findViewById(R.id.tvLatitude); 
    longitudeText = (TextView) findViewById(R.id.tvLongitude); 


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

    locationRequest = new LocationRequest(); 
    locationRequest.setInterval(10 * 1000); 
    locationRequest.setFastestInterval(15 * 1000); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
    requestLocationUpdates(); 
    } 

    private void requestLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
    latitudeText.setText("Connection Suspended"); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    latitudeText.setText("Connection Failed"); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
    myLatitude = location.getLatitude(); 
    mylongitude = location.getLongitude(); 

    latitudeText.setText("Latitude : " + String.valueOf(myLatitude)); 
    //latitudeText.setText("Latitude : " + String.valueOf(myLatitude)); 
    longitudeText.setText("Longitude : " + String.valueOf(mylongitude)); 
    } 

    @Override 
    protected void onStart() { 
    super.onStart(); 
    googleApiClient.connect(); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    if (googleApiClient.isConnected()) { 
     requestLocationUpdates(); 
    } 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); 
    } 

    @Override 
    protected void onStop() { 
    super.onStop(); 
    googleApiClient.disconnect(); 
    } 
} 
+1

Bitte überprüfen Sie diese Links [Anfordern von Berechtigungen zur Laufzeit] (https://developer.android.com/training/permissions/requesting.html), [Beispielverwendung] (https://github.com/commonsguy/cw-omnibus/blob/master /Location/FusedNew/app/src/main/java/com/commonsware/android/weather2/AbstractGoogleApiClientActivity.java) – Blackkara

Antwort

0

Dies könnte auf die neuen Laufzeit-Berechtigungen in Android M in Beziehung gesetzt werden, dass der Code der Handhabung noch nicht ist, hatte ich einen solchen Fall eine Weile her, wo meine App in den meisten meiner Geräte laufen würde , aber wenn es auf neuen Telefonen (unter Verwendung von Android 6.0+) installiert wird, würde es abstürzen.

Die Ursache war die fehlende Verwendung von expliziten Erlaubnis Anforderungen in der Laufzeit.

Sie könnten versuchen, die folgenden:

 int currentapiVersion = android.os.Build.VERSION.SDK_INT; 

     int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); 

     Bundle tmpSavedInstanceState = savedInstanceState; 


     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

      Toast.makeText(this, "Please accept the permission so you can use the location services", Toast.LENGTH_LONG).show(); 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERM_REQUEST); 

     } else { 

      // No explanation needed, we can request the permission. 

      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERM_REQUEST); 

      // MY_PERM_REQUEST is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 



     if (permissionCheck == PackageManager.PERMISSION_GRANTED || currentapiVersion <= Build.VERSION_CODES.M) { 
      //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION.. 
      // use location services 
     } 

Der Antrag der Asynchron-Berechtigungen kehrt Aufruf dieses Callback:

@Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERM_REQUEST: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 


        //do whatever you want with your location API 
        // everything went well, use the location services 


       } else { 

        // permission denied. Disable the 
        // functionality that depends on this permission. 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 

Sie könnten auch die documentation von @Blackkara erwähnt überprüfen

Let Ich weiß, wenn das half =)

+0

@Nathan O'Kane konnten Sie Ihr Problem lösen? lass es mich wissen, wenn das half =) – HenriqueMS

Verwandte Themen