Skip to main content

Permission For Marshmallow.


public class MarshMallowPermission {

    public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
    public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
    public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
    public static final int GPS_FINE_LOCATION_PERMISSION_REQUEST_CODE = 4;
    public static final int GPS_COARSE_LOCATION_PERMISSION_REQUEST_CODE = 5;
    public static final int PHONE_CALL_PERMISSION_REQUEST_CODE = 6;
    public static final int SEND_SMS_PERMISSION_REQUEST_CODE = 7;


    Activity activity;

    public MarshMallowPermission(Activity activity) {
        this.activity = activity;
    }

    public boolean checkPermissionForRecord() {
        int result = ContextCompat.checkSelfPermission(activity,
         Manifest.permission.RECORD_AUDIO);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public boolean checkPermissionForExternalStorage() {
        int result = ContextCompat.checkSelfPermission(activity,
 Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public boolean checkPermissionForCamera() {
        int result = ContextCompat.checkSelfPermission(activity,
     Manifest.permission.CAMERA);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public boolean checkPermissionForFineLocation() {
        int result = ContextCompat.checkSelfPermission(activity
Manifest.permission.ACCESS_FINE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public boolean checkPermissionForCoarseLocation() {
        int result = ContextCompat.checkSelfPermission(activity
      Manifest.permission.ACCESS_COARSE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
    }


    public boolean checkPermissionForDial() {
        int result = ContextCompat.checkSelfPermission(activity
      Manifest.permission.CALL_PHONE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public boolean checkPermissionForSendSMS() {
        int result = ContextCompat.checkSelfPermission(activity,
     Manifest.permission.SEND_SMS);
        return result == PackageManager.PERMISSION_GRANTED;
    }


    public void requestPermissionForRecord() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
       Manifest.permission.RECORD_AUDIO)) {
//            Toast.makeText(activity, "Microphone 
    permission needed for recording. Please allow in App Settings for additional 
    functionality.", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to Record feature";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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

    public void requestPermissionForExternalStorage() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//            Toast.makeText(activity, "External Storage permission needed. 
Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to Storage";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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

    public void requestPermissionForDial() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity
        Manifest.permission.CALL_PHONE)) {
//            Toast.makeText(activity, "External Storage permission needed. 
      Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to Storage";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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

    public void requestPermissionForCamera() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
      Manifest.permission.CAMERA)) {
//            Toast.makeText(activity, "Camera permission needed. 
      Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to Camera feature";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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

    public void requestPermissionForGPS() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity
         Manifest.permission.ACCESS_FINE_LOCATION)) {
//            Toast.makeText(activity, "GPS permission needed. 
      Please allow in App Setting for additional functionality", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to GPS feature";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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

    public void requestPermissionForGPS1() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity
           Manifest.permission.ACCESS_COARSE_LOCATION)) {
//            Toast.makeText(activity, "GPS permission needed. 
     Please allow in App Setting for additional functionality", Toast.LENGTH_LONG).show();

            String message = "You need to grant access to GPS feature";
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override

                        public void onClick(DialogInterface dialog, int which) {
                            if (activity == null)
                                return;

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


    public void requestPermissionsForCameraNew() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(activity,
     new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionsForExternalStorageNew() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(activity,
    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
     EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionsForDialNew() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(activity,
   new String[]{Manifest.permission.CALL_PHONE}, PHONE_CALL_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionsForFineLocationNew() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(activity,
   new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
      GPS_FINE_LOCATION_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionsForSendSmsNew() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(activity,  
          new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQUEST_CODE);
        }
    }


    private void showMessageOKCancel(String message,
      DialogInterface.OnClickListener okListener) {
        if (activity == null)
            return;

        new AlertDialog.Builder(activity)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

}
 
 
============================================================================================
 
 
 
Access This Class  in Your Class means .Java file 



MarshMallowPermission marshMallowPermission = new MarshMallowPermission(YourActivity.this);

// request cameraif (!marshMallowPermission.checkPermissionForCamera()) {
    marshMallowPermission.requestPermissionForCamera();
    return;
}

Comments

Popular posts from this blog

Android Logging using Timber Library

  Timber     is a logger with a small, extensible API which provides utility on top of Android's normal Log class. Before  the release, we’ll cleanup the log statements by removing them manually (even though logs can be disabled in release build). This tedious process can be avoided easily by using Timber. Timber provides lots of other options as well. Let’s see how it can be used in our projects to maintain the logs better. 1. Timber Below are few debug statements printed using default Log class. int a = 100 ; Log.e( "TAG" , String.format( "Integer a value is: %d" , a));   String name = "Android Codest" ; Log.e( "TAG" , String.format( "Find source from: %s" , name)); The above same statements can be printed using Timber as below. int a = 100 ; Timber.d( "Integer a value is: %d" , a);   String name = " Android Codest " ; Timber.d( " Find source from : %s" , name); You can notice here, the TAG is not p...

Convert your apk into Android App Bundle

Android App Bundle In Google I/O 2018, a new publishing format has been introduced for Android applications called Android App Bundle. It is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play. Traditionally, Android apps are distributed using a special file called an Android Package(.apk). How to build an app bundle? You can easily build your app bundle using Android Studio(3.2 Canary 14+) or using command line interface. The generated app bundle will be stored at app/build/outputs/bundle/buildVariant/bundle.aab. Android Studio : Go to Build > Build Bundle(s)/APK(s) and select Build Bundle(s). Console : ./gradlew bundle Dynamic Delivery with Split APKs A fundamental component of Dynamic Delivery is the split APK mechanism available on Android 5.0 (API level 21) and higher. Split APKs are very similar to regular APKs — they include compiled DEX bytecode, resources, and an Android manifest. However, the ...

Push Notification using Firebase in Android

This is a tutorial about sending push notifications to Android through Firebase, based on the new release of Firebase this year (2016). This tutorial shows how to setup the skeleton for sending and receiving push notifications via FCM with instructions on server code. 1. Get started Add a new project or import an existing project to  Firebase console . If you choose to create a new project, you need to set the project name and country. For example, I will call my project Firebase Notification Then select "Add Firebase to your Android app". Set a package name for your app. I only set my package name and omit the SHA-1 because I don't use Firebase for my app's authentication. Click the  ADD APP  button here to download google-services.json. This is an important file and you will need to put it into your app. 2. Add google-services.json to your app folder Replace the google-services.json in your app folder. The Google services plugi...