Java Code Examples for android.app.Activity#requestPermissions()

The following examples show how to use android.app.Activity#requestPermissions() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@TargetApi(M)
static private void getPermission(String[] permissions, Activity activity) {
    if(Build.VERSION.SDK_INT>=23) {
        ArrayList<String> preToDo = new ArrayList<>();
        boolean tip = false;
        for (String pre : permissions) {
            if (activity.checkSelfPermission(pre) != PackageManager.PERMISSION_GRANTED) {
                preToDo.add(pre);
                if (activity.shouldShowRequestPermissionRationale(pre)) {
                    tip = true;
                }
            }
        }
        if (preToDo.size() == 0)
            return;
        if (tip)
            showToast(sContext.getString(R.string.permissions_need));
        activity.requestPermissions(preToDo.toArray(new String[preToDo.size()]), 0);
    }
}
 
Example 2
Source Project: recent-images   File: Util.java    License: MIT License 6 votes vote down vote up
public static void checkPermission(Activity activity, String[] permissions,
                                   OnPermissionCallback permissionsResultCallback) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        boolean req = false;

        for (int i = 0; i < permissions.length; i++) {
            if (activity.checkSelfPermission(permissions[i]) == PackageManager.PERMISSION_DENIED) {
                req = true;
            }
        }
        if (!req)
            permissionsResultCallback.onPermissionGranted();
        else {
            permissionMap.clear();

            permissionStringRes = R.string.pleaseEnablePermission;
            permissionMap.put(PERMISSION_REQUEST_CODE, new Pair<>(new Pair<>(activity,
                    permissionsResultCallback), permissions));

            activity.requestPermissions(permissions, PERMISSION_REQUEST_CODE);

        }
    } else {
        permissionsResultCallback.onPermissionGranted();
    }
}
 
Example 3
Source Project: K-Sonic   File: Util.java    License: MIT License 6 votes vote down vote up
/**
 * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE}
 * permission read the specified {@link Uri}s, requesting the permission if necessary.
 *
 * @param activity The host activity for checking and requesting the permission.
 * @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read.
 * @return Whether a permission request was made.
 */
@TargetApi(23)
public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) {
  if (Util.SDK_INT < 23) {
    return false;
  }
  for (Uri uri : uris) {
    if (Util.isLocalFileUri(uri)) {
      if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE)
          != PackageManager.PERMISSION_GRANTED) {
        activity.requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE}, 0);
        return true;
      }
      break;
    }
  }
  return false;
}
 
Example 4
Source Project: WanAndroid   File: PermissionUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 动态申请权限
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public static void requestPermission(Activity context, String... permissions) {
    if (permissions.length > 0) {
        context.requestPermissions(permissions, REQUEST_CODE);
    }
}
 
Example 5
Source Project: IPTVFree   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Request permission on android M
 * @param activity Activity
 */
public void requestWriteExternalStoragePermission(Activity activity)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int hasWriteExternalStoragePermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        List<String> permissions = new ArrayList<String>();
        if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (!permissions.isEmpty()) {
            activity.requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSIONS);
        }
    }
}
 
Example 6
public static void startCall(TLRPC.User user, final Activity activity, TLRPC.TL_userFull userFull){
	if(userFull!=null && userFull.phone_calls_private){
		new AlertDialog.Builder(activity)
				.setTitle(LocaleController.getString("VoipFailed", R.string.VoipFailed))
				.setMessage(AndroidUtilities.replaceTags(LocaleController.formatString("CallNotAvailable", R.string.CallNotAvailable,
						ContactsController.formatName(user.first_name, user.last_name))))
				.setPositiveButton(LocaleController.getString("OK", R.string.OK), null)
				.show();
		return;
	}
	if (ConnectionsManager.getInstance(UserConfig.selectedAccount).getConnectionState() != ConnectionsManager.ConnectionStateConnected) {
		boolean isAirplaneMode = Settings.System.getInt(activity.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
		AlertDialog.Builder bldr = new AlertDialog.Builder(activity)
				.setTitle(isAirplaneMode ? LocaleController.getString("VoipOfflineAirplaneTitle", R.string.VoipOfflineAirplaneTitle) : LocaleController.getString("VoipOfflineTitle", R.string.VoipOfflineTitle))
				.setMessage(isAirplaneMode ? LocaleController.getString("VoipOfflineAirplane", R.string.VoipOfflineAirplane) : LocaleController.getString("VoipOffline", R.string.VoipOffline))
				.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
		if (isAirplaneMode) {
			final Intent settingsIntent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
			if (settingsIntent.resolveActivity(activity.getPackageManager()) != null) {
				bldr.setNeutralButton(LocaleController.getString("VoipOfflineOpenSettings", R.string.VoipOfflineOpenSettings), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						activity.startActivity(settingsIntent);
					}
				});
			}
		}
		bldr.show();
		return;
	}
	if (Build.VERSION.SDK_INT >= 23 && activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
		activity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 101);
	} else {
		initiateCall(user, activity);
	}
}
 
Example 7
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (activity.shouldShowRequestPermissionRationale(
                        Manifest.permission.ACCESS_FINE_LOCATION)) {
                    AlertDialog.Builder builder =
                            new AlertDialog.Builder(activity);
                    builder.setMessage(activity.getString(R.string.permissions_location));
                    builder.setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    activity.requestPermissions(new String[]{
                                                    Manifest.permission.ACCESS_FINE_LOCATION},
                                            FINE_LOCATION_PERMISSION_REQUESTCODE);
                                }
                            }
                    );
                    // display the dialog
                    builder.create().show();
                } else {
                    // request permission
                    activity.requestPermissions(
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            FINE_LOCATION_PERMISSION_REQUESTCODE);
                }
            }
        }
    }
}
 
Example 8
Source Project: AwesomeImagePicker   File: Helper.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean requestStoragePermission(Activity activity, int READ_STORAGE_PERMISSION) {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    READ_STORAGE_PERMISSION);
        }
    } else {
    }
    return false;
}
 
Example 9
@TargetApi(23)
public static boolean requestPermission(Activity activity, String permission, int rationale, int requestCode) {
    if (shouldNotRequestPermission(activity, permission, rationale))
        return true;
    activity.requestPermissions(new String[]{permission}, requestCode);
    Bundle bundle = new Bundle();
    bundle.putString("permission", permission);
    bundle.putBoolean("never_ask", PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("never_ask_checked", false));
    return false;
}
 
Example 10
Source Project: SSForms   File: Tools.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean requestStoragePermission(Activity activity, int READ_STORAGE_PERMISSION) {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    READ_STORAGE_PERMISSION);
        }
    } else {
    }
    return false;
}
 
Example 11
Source Project: YImagePicker   File: PPermissionUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean hasCameraPermissions(Activity activity) {
    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{Manifest.permission.CAMERA}, ImagePicker.REQ_CAMERA);
            return false;
        }
    }
    return true;
}
 
Example 12
Source Project: YImagePicker   File: PPermissionUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean hasCameraPermissions(Activity activity) {
    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{Manifest.permission.CAMERA}, ImagePicker.REQ_CAMERA);
            return false;
        }
    }
    return true;
}
 
Example 13
Source Project: YImagePicker   File: PPermissionUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean hasStoragePermissions(Activity activity) {
    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ImagePicker.REQ_STORAGE);
            return false;
        }
    }
    return true;
}
 
Example 14
public static void startCall(TLRPC.User user, final Activity activity, TLRPC.UserFull userFull) {
	if (userFull != null && userFull.phone_calls_private) {
		new AlertDialog.Builder(activity)
				.setTitle(LocaleController.getString("VoipFailed", R.string.VoipFailed))
				.setMessage(AndroidUtilities.replaceTags(LocaleController.formatString("CallNotAvailable", R.string.CallNotAvailable,
						ContactsController.formatName(user.first_name, user.last_name))))
				.setPositiveButton(LocaleController.getString("OK", R.string.OK), null)
				.show();
		return;
	}
	if (ConnectionsManager.getInstance(UserConfig.selectedAccount).getConnectionState() != ConnectionsManager.ConnectionStateConnected) {
		boolean isAirplaneMode = Settings.System.getInt(activity.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
		AlertDialog.Builder bldr = new AlertDialog.Builder(activity)
				.setTitle(isAirplaneMode ? LocaleController.getString("VoipOfflineAirplaneTitle", R.string.VoipOfflineAirplaneTitle) : LocaleController.getString("VoipOfflineTitle", R.string.VoipOfflineTitle))
				.setMessage(isAirplaneMode ? LocaleController.getString("VoipOfflineAirplane", R.string.VoipOfflineAirplane) : LocaleController.getString("VoipOffline", R.string.VoipOffline))
				.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
		if (isAirplaneMode) {
			final Intent settingsIntent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
			if (settingsIntent.resolveActivity(activity.getPackageManager()) != null) {
				bldr.setNeutralButton(LocaleController.getString("VoipOfflineOpenSettings", R.string.VoipOfflineOpenSettings), (dialog, which) -> activity.startActivity(settingsIntent));
			}
		}
		bldr.show();
		return;
	}
	if (Build.VERSION.SDK_INT >= 23 && activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
		activity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 101);
	} else {
		initiateCall(user, activity);
	}
}
 
Example 15
public static void requestPermissions(Activity activity, String[] permissions, int requestCode) {
    if (useRunTimePermissions()) {
        activity.requestPermissions(permissions, requestCode);
    }
}
 
Example 16
/**
 * Check to see we have the necessary permissions for this app, and ask for them if we don't.
 */
public static void requestPermissions(Activity activity) {
    activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE_PERMISSION, RECORD_AUDIO_PERMISSION},
            GRANT_REQUEST_CODE);
}
 
Example 17
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.GET_ACCOUNTS) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (canAskPermission) {
                    if (activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.GET_ACCOUNTS)) {
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(activity);
                        builder.setMessage(activity.getString(R.string.permissions_accounts));
                        builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (canAskPermission) {
                                            activity.requestPermissions(new String[]{
                                                            Manifest.permission.GET_ACCOUNTS},
                                                    GET_ACCOUNTS_PERMISSION_REQUESTCODE);
                                        }
                                    }
                                }
                        );
                        // display the dialog
                        builder.create().show();
                    } else {
                        // request permission
                        if (canAskPermission) {
                            activity.requestPermissions(
                                    new String[]{Manifest.permission.GET_ACCOUNTS},
                                    GET_ACCOUNTS_PERMISSION_REQUESTCODE);
                        }
                    }
                }
            }
        }
    }
}
 
Example 18
Source Project: openapk   File: OtherUtils.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static void requestPermissions(Context context) {
    Activity activity = (Activity) context;
    if (!checkPermissions(context)) {
        activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, AppPreferences.CODE_PERMISSION);
    }
}
 
Example 19
/**
 * requestPermissions single
 * shouldShowRequestPermissionRationale
 * 1.上次选择禁止并勾选:下次不在询问	false
 * 2.第一次打开App时	 false
 * 3.上次弹出权限点击了禁止(但没有勾选“下次不在询问”)	 true
 *
 * @param mActivity
 * @param permission
 * @param requestCode
 * @param requestPermissionsCallBcak
 */
public static void requestPermissions(Activity mActivity, String permission, int requestCode, RequestPermissionsCallBcak requestPermissionsCallBcak) {
    //判断权限是否开启
    if (ContextCompat.checkSelfPermission(mActivity, permission) != PackageManager.PERMISSION_GRANTED) {
        mActivity.requestPermissions(new String[]{permission}, requestCode);
    } else {
        //已经拥有权限,可以做自己想做的操作
        requestPermissionsCallBcak.requestPermissionsSucesss(requestCode);
    }
}
 
Example 20
Source Project: DeviceConnect-Android   File: PermissionUtil.java    License: MIT License 2 votes vote down vote up
/**
 * パーミッションを要求します.
 *
 * @param activity リクエストレスポンスを受け取るActivity
 * @param permissions 許可を求めるパーミッションの配列
 * @param requestCode リクエストコード
 */
public static void requestPermissions(final Activity activity, final String[] permissions, final int requestCode) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        activity.requestPermissions(permissions, requestCode);
    }
}