Advertisement
Guest User

Untitled

a guest
May 30th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. public static final int REQUIRED_PERMISSIONS = 001;
  4.  
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  10.  
  11. //give here permissions what ever you want...
  12.  
  13. if CheckPermission(this, Manifest.permission.CAMERA)
  14.  
  15. {
  16. PermHandling();
  17. }
  18.  
  19. //now reqest runtime permissions..
  20.  
  21. else {
  22. RequestPermission(MainActivity.this, Manifest.permission.CAMERA, REQUIRED_PERMISSIONS);
  23. }
  24.  
  25. }
  26. private void PermHandling() {
  27. //Your app internal parts....
  28. //Here your stuff works...
  29. }
  30.  
  31. //private void NewPermHandling(){
  32.  
  33. //}
  34.  
  35. @Override
  36. public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
  37.  
  38. switch (permsRequestCode) {
  39.  
  40. case REQUIRED_PERMISSIONS: {
  41. if (grantResults.length > 0
  42. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  43. PermHandling();
  44. } else {
  45. //Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
  46.  
  47. /*AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  48. builder.setMessage("Note: Please Grant Permissions");
  49. builder.setTitle("Grant Permissions Again");
  50. builder.setPositiveButton("OK",null);
  51.  
  52. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  53. @Override
  54. public void onClick(DialogInterface dialog, int which) {
  55.  
  56. dialog.dismiss();
  57. Intent la = new Intent(Qdata.this, AQdata.class);
  58. startActivity(la);
  59. finish();
  60. }
  61. });
  62. AlertDialog dialog = builder.create();
  63. dialog.show();*/
  64.  
  65. }
  66. return;
  67. }
  68. }
  69. }
  70.  
  71. public void RequestPermission(Activity thisActivity, String Permission, int Code) {
  72. if (ContextCompat.checkSelfPermission(thisActivity,
  73. Permission)
  74. != PackageManager.PERMISSION_GRANTED) {
  75. if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
  76. Permission)) {
  77. } else {
  78. ActivityCompat.requestPermissions(thisActivity,
  79. new String[]{Permission},
  80. Code);
  81. }
  82. }
  83. }
  84.  
  85. public boolean CheckPermission(Context context, String Permission) {
  86. if (ContextCompat.checkSelfPermission(context,
  87. Permission) == PackageManager.PERMISSION_GRANTED) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement