How can I easily make application location permission?

Asked

Viewed 498 times

4

I found this tutorial https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en but I didn’t understand anything.

I looked at this tutorial:

// Here, thisActivity is the current activity
   if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

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

        // 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.

    } else {

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

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}
Handle the permissions request response

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

            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {

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

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

}

I don’t understand what the variable MY_PERMISSIONS_REQUEST_READ_CONTACTS is for.

How do you make application location permission?

  • Aline, I created a lib that makes it much easier for me. Always in my projects I use. It works as follows, declaring the Permissmanager class, I only call the method corresponding to the type of permission. If you’re interested, I can go into more detail.

  • I have an interest

2 answers

1

I don’t understand what the variable MY_PERMISSIONS_REQUEST_READ_CONTACTS is for.

Permission request is made with a call to the method ActivityCompat.requestPermissions(). The response is then received in the method onRequestPermissionsResult().

An application can ask for more than one permission and these requests can be made at different times.
How all responses are received in the method onRequestPermissionsResult() it is necessary to have a way of knowing to which request it refers.

The method requestPermissions(Activity activity, String[] permissions, int requestCode) receives, in the third parameter, an integer whose purpose is to identify the request(requestCode).
When the response is received, it can be obtained in the first parameter of the method onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults), which makes it possible to know to which request the reply relates.

The field MY_PERMISSIONS_REQUEST_READ_CONTACTS is the value that identifies the request. If you check the code, in the unpublished part, you will see that it has been declared as int and has an assigned value. The value itself does not matter, only that it is different from any other used to request permissions.

How do you make application location permission?

The simplest code is the one you posted, however it refers to a request for permission of the type Manifest.permission.READ_CONTACTS.

To ask for permission to know the location of the application you will need to change where it is Manifest.permission.READ_CONTACTS for Manifest.permission.ACCESS_COARSE_LOCATION or Manifest.permission.ACCESS_FINE_LOCATION.

You should also change the name of the constant MY_PERMISSIONS_REQUEST_READ_CONTACTS or create another with an appropriate name, for example MY_PERMISSIONS_REQUEST_ACCESS_LOCATION.

0

From the Android Marshmallow, API 23, users grant permissions to applications while they are running, not when they are installed.

This approach optimizes the application installation process, as the user does not need to grant permissions when installing or updating app.

In addition to this issue, it also gives the user more control over the features of the application. For example, a user could choose to allow a camera app access to the camera, but not the location of the device. The user can revoke permissions at any time on the application settings screen.

As shown at the time you grant permission:

inserir a descrição da imagem aqui

How can I make application location permission in a way simple?

At first I had these same difficulties that you are having, and not to face it again I created a lib, precisely to grant these permissions as simply as possible.

The lib is on Github with the name Permission, if you wanted to use, modify, test, etc.;

See below example, in this case granting location permission.

/**
 * This method request permission for get location
 * 
 * @return false/true
 */
public boolean requestPermissLocation() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && ActivityCompat.checkSelfPermission(activity,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(activity,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

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

        Log.i(TAG, "PERMISSION ACCESS LOCATION");

        return false;
    } else
        return true;
}

The classes ContextCompat and ActivityCompat allow us to keep the code working for versions prior to Android 6.0.

The method checkSelfPermission() takes the current context as a parameter (in our case the Activity) and the permission we must verify for the following features to be used without problems granted. If the returned value is different from PackageManager.PERMISSION_GRANTED we should be on probation.

The method requestPermissions() is responsible for showing the Dialog permission native, presented just above this reply. In cases where the CheckBox "Never Ask Again" has previously been selected or permission has already been granted to Dialog is ignored and nothing is presented. Note that the second parameter is a String array that contains all permissions required to execute the functionality the user requested in the APP.

I don’t understand what the variable is for MY_PERMISSIONS_REQUEST_READ_CONTACTS.

The MY_PERMISSIONS_REQUEST_READ_CONTACTS, in this case it would be the third parameter of the method requestPermissions(), which is a whole of 8 bits which will be used in the method onRequestPermissionsResult() to verify whether permission has been granted or not, if yes, in the same method can be called the functionality requested by the user.

For the use of lib Permission you will declare the class this way:

PermissManager permiss = new PermissManager(this);

Ai for verification, you use the following condition:

if (permiss.requestPermissLocation()) {
    // Aqui você insere a instrução desejada 
}

In the lib also contains the following methods:

  • requestPermissForRecord()
  • requestPermissLocation()
  • requestPermissExtStorage()
  • requestPermissCamera()
  • requestPermissContacts()
  • requestPermissPhone()
  • requestPermissCalendar()
  • requestPermissSensors()
  • requestPermissSMS()

You can check more details about permission in the documentation.

  • @Aline managed to solve its problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.