4/5/2018 - 6:03 AM

User Permissions

Taking user permissions for the access of the user data like IMEI number,location etc...

  //First Add the required Uses Permissions in the Manifest File
  // Asking the user for permissions to take IMEI number and user current location.(you can declare more permissions in the new String[] {})
      //do this in onCreate();
    android.support.v4.app.ActivityCompat.requestPermissions(this, new String[]{READ_PHONE_STATE,ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
    //Now check wether the user have granted permissions or not 
     // this is method to track weather the user has allowed the permission / Denied the permission.
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted,by the user
                    //Do what ever you waht if the user grants permission.

                } else {
                    if (sharedPrefService.getString(IMEI_NO) != null ){
                        // permission denied by the user he cannot get inside the app
                        //do what ever you want to do if user does not grant permission.
    //if you want to verify again ether the user have given permission or not then use the below code to verify again
    //This is only mandatery when we are taking PHONE STATE permission
    //if the user have not taken permission this method will ask the user for permission again.
      public void getUserPhoneDetails() {

        if (ContextCompat.checkSelfPermission(SplashActivity.this, READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {

            telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            imeiNo = telephonyManager.getDeviceId();
            sharedPrefService.setString(IMEI_NO, imeiNo);

        } else {
            Toast.makeText(this, "permission denied", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{READ_PHONE_STATE}, PERMISSION_READ_STATE);