Permission issues for location in android Marshmallow applicaton

Straight forward solution/example for your question:

package com.my.packagename;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE_LOCATION = 1;

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

    if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),CopyOfMap.this)) {
        fetchLocationData();
    }
    else
    {
        requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),CopyOfMap.this);
    }
}

public static void requestPermission(String strPermission,int perCode,Context _c,Activity _a){

    if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
                Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
            } else {

                ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
            }
    }

public static boolean checkPermission(String strPermission,Context _c,Activity _a){
        int result = ContextCompat.checkSelfPermission(_c, strPermission);
        if (result == PackageManager.PERMISSION_GRANTED){

            return true;

        } else {

            return false;

        }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

        case PERMISSION_REQUEST_CODE_LOCATION:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                fetchLocationData();

            } else {

                Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();

            }
            break;

    }
}


    private void fetchLocationData()
    {
    //code to use the granted permission (location)
    }
}

Here i've made the requestPermission() and checkPermission() method as public static because i can reuse them for other permission requests like phone call, write storage, and others. Hope this will help some one. :-)


In Marshmallow there are lots of new things in android SDK. Asking the permission is one of it's new update that every developer should keep in mind. The similar thing happen with you. if your application target version 23 then you probably should have to take grant from the user before start to use it. You can only use it if user allow to access.Therefor developer need to take care all the test-case while taking the permission from the user into the application

For example your application need the access of the Location. In earlier version of android at time on installed it was show the permission dialog and they were granted the defined permission in your manifest file to the application. This things changed into the Marshmallow. Now each application should have to take permission from user to access the it.

See the developer sites and learn the basic steps for how to guide the user into the application for asking the runtime permission into the application.

There are lots of sites showing your the basic tutorial about how to ask the permission in Marshmallow. few can suggest as below

Building better application with runtime permission

Permissions design guide

This link guide you the basic info of how to guide the user into the application before asking the permission.

Thanks.