在运行时请求位置权限不起作用

时间:2017-12-09 12:06:04

标签: android

我已经从this documentation写了我的代码。单击按钮时会调用askForPermission()方法。但是每次我点击按钮都会显示其他部分,我的意思是Permission denied。我在这做错了什么。如何打开弹出窗口以获取请求权限。授予权限后,如何在TOAST消息中显示latitudelongitude。 ??我是android的新手,如果有人能解决它,我将非常感激。

我已尝试从LOCATION页面为我的应用程序启用mobile's setting权限。但是,当我点击按钮时,它会显示PERMISSION DENIED

  

events.java

package com.example.borntoflirt.design1;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class events extends AppCompatActivity {

    private Button getLocationBtn;
    public static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

    private FusedLocationProviderClient mFusedLocationClient;

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

        getLocationBtn = (Button) findViewById(R.id.getLocation);

        getLocationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //First of All check for permission .
                askForPermission();
            }
        });

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    }

    private void askForPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                // Show an explanation 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.
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;

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

}

1 个答案:

答案 0 :(得分:0)

这是您评论中的问题的答案。因此,要访问位置和纬度和经度,您需要在activity类中实现几个接口: implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener

然后覆盖必要的方法。在onConnected方法中,使用已弃用的FusedLocationApi或新的FusedLocationProviderClient向位置发出请求。此外,您将获得带有参数onLocationChanged的{​​{1}}方法,您可以使用Locationlocation.getLatitude();

访问纬度和经度

您还必须与location.getLongitude();建立连接,例如:

GoogleApiClient

处理protected synchronized void buildGoogleApiClient() { googleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); googleApiClient.connect(); }中的位置更新,并在onPause

中再次发出请求