Android Marshmallow Permissions Example – Android Coding by DMTechnolab

Simplified Coding


The package The trap.Simplified coding.Running;

Import Android.Clearly;

Import Android.Content.in the evening.package manager;

Import Android.Support.Annotation.No no;

Import Android.Support.v4.App.Activity.com;

Import Android.Support.v4.Content.Context.com;

Import Android.Support.v7.App.AppCompatActivity;

Import Android.The.Bundle;

Import Android.Support.v7.Opinion.Menu.ActionMenuItemView;

Import Android.Opinion.Opinion;

Import Android.Widget.switch;

Import Android.Widget.Toast;

public class Main activity Is spread AppCompatActivity {{

// our button

Private switch Button design;

// Permission code that will be tested in the onRequestPimsResult method

Private int STORAGE_PERMISSION_CODE = 23;

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);

setContentView()R.Layout.activity_main);

// initial button

Button design = ()switch) findViewById()R.Id.Button design);

// add a click listener

Button design.setOnClickListener()New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion v) {{

// first checking if the app is already allowing

If()isReadStorageAllowed())){{

// showing toast if permission is already there

Toast.Makeup()Main activity.this,“You already have permission”,Toast.LENGTH_LONG).Display());

// Returns the existing method with

The return;

}

// Ask for permission if the app is not allowed

requestStoragePermission());

}

});

}

// We are calling this method to check the status of the permission

Private Boolean isReadStorageAllowed()) {{

// getting permission status

int results = Context.com.checkSelfPermission()this, Clearly.Permission.READ_EXTERNAL_STORAGE);

// if permission is returned true

If ()results == package manager.permission granted)

The return True;

// false permission is allowed if not allowed

The return false;

}

// permission required

Private Zero requestStoragePermission()){{

If ()Activity.com.shouldShowRequestPermissionRationale()this,Clearly.Permission.READ_EXTERNAL_STORAGE)){{

// If the user has refused permission, your code will fall into this block

// Here you can explain why you need this permission

// explain here why you need this permission

}

// and finally ask for permission

Activity.com.Request()this,New String[]{{Clearly.Permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);

}

// This method will be called when the user taps on permission or denial

@ Override

public Zero onRequestPcriResult()int request code, @Nonal String[] Permissions, @Nonal int[] Grant) {{

// checking the request code of our request

If()request code == STORAGE_PERMISSION_CODE){{

// if allowed

If()Grant.Length >0 && Grant[0] == package manager.permission granted){{

// Displaying a toast

Toast.Makeup()this,“Permission now you can read storage”,Toast.LENGTH_LONG).Display());

}other{{

// Displaying the second toast if not allowed

Toast.Makeup()this,“Oops you just refused permission”,Toast.LENGTH_LONG).Display());

}

}

}

}



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*