Marshmallow Permissions

Marshmallow permissions:

Before marshmallow devices ,permissions were given statically i.e., developers give permissions in manifest.xml file. If app contains any permission which will be available only before installing(see picture).Giving all permission before install have some drawback.Some users doesn't want to allow some permissions.In other words some users doesn't give access some permissions.For example user doesn't want to give access to app to find current location.

In marshmallow,when app need current location at particular page ,ask permission at run time,user may or may not grant permissions.Granted permission can be checked in onActivityResult() method. So that asking permission at run time permissions, security will be increased,can be understood by user,efficiency of app will be increased.

All permission were given before install before marshmallow devices.

public class MarshmallowPermissionActivity extends AppCompatActivity {

private String TAG="LokeshMPA";

private Button btClickButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_marshmallow_permission);

}

public void checkPermission(View ve) {

// here checking permissions, following block will be true when permission is not granted

// you can also check multiple permissions wether permission is granted or not. I checked only one permission

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)

!= PackageManager.PERMISSION_GRANTED){

//Following if block will be executed when permission canceled by user after firsttime.

//Note: First time the following if block will not be executed.

if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)){

// here you can give your dialog with ok and canel buttons,when ok clicked,again request(ask) permission

//i.e., ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);

}

}else{

// request permissions

// you can also request two or more permissions in String[]{1,2,3,4}. Here I gave only one permission

// i.e. new String[1]

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

}

}

// this method will be called after permissions dialog closed.

// If permission granted ,permissions results will be sent to 'grantResult' parameter in following method.

@Override

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

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == 100) {

for (int i = 0; i < grantResults.length; i++) {

if (grantResults.length > 0 & grantResults[i] == PackageManager.PERMISSION_GRANTED) {

Toast.makeText(this, "Permission Granted for "+permissions[i], Toast.LENGTH_SHORT).show();

}

}

}

}

}

activity_marshmallow_permission.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="check runtime permission"

android:onClick="checkPermission"/>

</LinearLayout>

Manifest.xml

put : <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

output:

After giving permissions in marshmallow device