Android位置服务运行时权限错误

时间:2016-12-13 17:19:39

标签: android locationmanager runtime-permissions

即使在根据以下链接处理运行时权限

后,我也无法Gradle构建我的应用程序

https://developers.google.com/android/guides/permissions

我收到“无法解析符号”locationListener“和消息”呼叫需要可能被用户拒绝的权限“

您能否协助解决该计划的错误或哪些情况未得到妥善处理

主要活动文件

公共类MainActivity扩展AppCompatActivity实现了GoogleApiClient.ConnectionCallbacks,         GoogleApiClient.OnConnectionFailedListener,LocationListener {

private final String TAG = getClass().getSimpleName();
private GoogleMap mMap;
private String[] places;
private LocationManager locationManager;
private Location loc;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
String lat, lon;
Location mLastLocation;


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

    if (BuildConfig.VERSION_CODE < 23) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
    } else {

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        {
            //Request for Permission
          // ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)){
                // Display UI and wait for user interaction
            }
            else {
                ActivityCompat.requestPermissions(
                        this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        1);
            }
        } else {
            // permission has been granted, continue as usual
            Location myLocation =
                    LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        }
    }

}

protected synchronized void buildGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}
@Override
protected void onStart() {
    super.onStart();
    // Connect the client.
    Log.d(TAG, "onStart fired ..............");
    mGoogleApiClient.connect();

}

@Override
protected void onStop() {
    // Disconnecting the client invalidates it.
    mGoogleApiClient.disconnect();
    Log.d(TAG, "onStop fired ..............");

    super.onStop();
}


@Override
public void onConnected(Bundle bundle) {

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000); // Update location every 10 seconds

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lon = String.valueOf(mLastLocation.getLongitude());
    }}

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

    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
    {
                    Location myLocation =
                    LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);            
       }
    }

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "GoogleApiClient connection has been suspend");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "GoogleApiClient connection has failed");
}
@Override
protected void onPause() {
    super.onPause();
}
@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, location.toString());
    //txtOutput.setText(location.toString());

    //txtOutput.setText(Double.toString(location.getLatitude()));
}

}

0 个答案:

没有答案