Example usage for android.app Activity requestPermissions

List of usage examples for android.app Activity requestPermissions

Introduction

In this page you can find the example usage for android.app Activity requestPermissions.

Prototype

public final void requestPermissions(@NonNull String[] permissions, int requestCode) 

Source Link

Document

Requests permissions to be granted to this application.

Usage

From source file:Main.java

public static void requestPermissions(Activity context, int requestCode) {
    context.requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, requestCode);
}

From source file:org.sufficientlysecure.keychain.ui.util.PermissionsUtil.java

/**
 * Request READ_EXTERNAL_STORAGE permission on Android >= 6.0 to read content from "file" Uris.
 * <p/>//from w ww  .j a v  a  2 s .  co m
 * This method returns true on Android < 6, or if permission is already granted. It
 * requests the permission and returns false otherwise.
 * <p/>
 * see https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html
 */
@SuppressLint("NewApi") // Api level is checked in checkReadPermission
public static boolean checkAndRequestReadPermission(Activity activity, Uri uri) {
    boolean result = checkReadPermission(activity, uri);
    if (!result) {
        activity.requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                PERMISSION_READ_EXTERNAL_STORAGE);
    }
    return result;
}

From source file:dg.shenm233.mmaps.util.PermissionUtils.java

@TargetApi(Build.VERSION_CODES.M)
public static void requestPermissionsAndThen(Activity activity, int requestCode, String[] perms,
        PermsCallback permsCallback) {//from   www  . j  a va 2  s .c o m
    if (Build.VERSION.SDK_INT < 23 || checkPermissions(activity, perms)) {
        permsCallback.onAllGranted();
        return;
    }

    OnRequestPermissionsResult r = prepareRequest(activity, requestCode, perms, permsCallback);
    activity.requestPermissions(r.getRequestPerms(), requestCode);
}

From source file:ti.permissions.TipermissionsModule.java

private boolean handleRequest(String[] permissions, KrollFunction permissionCallback) {
    Activity activity = TiApplication.getAppCurrentActivity();

    if (TiBaseActivity.cameraCallbackContext == null) {
        TiBaseActivity.cameraCallbackContext = getKrollObject();
    }//from  w w w  . j  a  v a2s  .c  om
    TiBaseActivity.cameraPermissionCallback = permissionCallback;

    Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
    currentActivity.requestPermissions(permissions, TiC.PERMISSION_CODE_CAMERA);

    return true;
}

From source file:ti.notificare.NotificareTitaniumAndroidModule.java

/**
 * Enable location updates/*from   w w  w  .  ja  v a2s  . c  om*/
 */
@Kroll.method
public void enableLocationUpdates(@Kroll.argument(optional = true) KrollFunction permissionCallback) {
    if (Notificare.shared().hasLocationPermissionGranted()) {
        Notificare.shared().enableLocationUpdates();
    } else {
        Log.i(TAG, "location permission not granted");
        if (TiBaseActivity.locationCallbackContext == null) {
            TiBaseActivity.locationCallbackContext = getKrollObject();
        }
        TiBaseActivity.locationPermissionCallback = permissionCallback;
        String[] permissions = new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION };
        Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
        currentActivity.requestPermissions(permissions, TiC.PERMISSION_CODE_LOCATION);
    }
}

From source file:de.baumann.browser.helper.helper_main.java

public static void grantPermissionsLoc(final Activity from) {

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from);

    if (android.os.Build.VERSION.SDK_INT >= 23) {

        if (sharedPref.getBoolean("perm_notShow", false)) {
            int hasACCESS_FINE_LOCATION = from.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            if (hasACCESS_FINE_LOCATION != PackageManager.PERMISSION_GRANTED) {
                if (!from.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    new AlertDialog.Builder(from).setTitle(R.string.app_permissions_title)
                            .setMessage(helper_main.textSpannable(from.getString(R.string.app_permissions)))
                            .setNeutralButton(R.string.toast_notAgain, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    sharedPref.edit().putBoolean("perm_notShow", false).apply();
                                }/* w  w  w.ja v a 2  s  . co m*/
                            }).setPositiveButton(from.getString(R.string.toast_yes),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (android.os.Build.VERSION.SDK_INT >= 23)
                                                from.requestPermissions(
                                                        new String[] {
                                                                Manifest.permission.ACCESS_FINE_LOCATION },
                                                        REQUEST_CODE_ASK_PERMISSIONS_1);
                                        }
                                    })
                            .setNegativeButton(from.getString(R.string.toast_cancel), null).show();
                    return;
                }
                from.requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                        REQUEST_CODE_ASK_PERMISSIONS_1);
            }
        }
    }
}

From source file:de.baumann.browser.helper.helper_main.java

public static void grantPermissionsStorage(final Activity from) {

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from);

    if (android.os.Build.VERSION.SDK_INT >= 23) {
        if (sharedPref.getBoolean("perm_notShow", false)) {
            int hasWRITE_EXTERNAL_STORAGE = from
                    .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
                if (!from.shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                    new AlertDialog.Builder(from).setTitle(R.string.app_permissions_title)
                            .setMessage(helper_main.textSpannable(from.getString(R.string.app_permissions)))
                            .setNeutralButton(R.string.toast_notAgain, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    sharedPref.edit().putBoolean("perm_notShow", false).apply();
                                }/*from  w ww.  j  av  a 2 s  .  co  m*/
                            }).setPositiveButton(from.getString(R.string.toast_yes),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (android.os.Build.VERSION.SDK_INT >= 23)
                                                from.requestPermissions(
                                                        new String[] {
                                                                Manifest.permission.WRITE_EXTERNAL_STORAGE },
                                                        REQUEST_CODE_ASK_PERMISSIONS);
                                        }
                                    })
                            .setNegativeButton(from.getString(R.string.toast_cancel), null).show();
                    return;
                }
                from.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                        REQUEST_CODE_ASK_PERMISSIONS);
            }
        }
    }
}

From source file:org.pouyadr.ui.LocationActivity.java

@Override
public void onResume() {
    super.onResume();
    AndroidUtilities.removeAdjustResize(getParentActivity(), classGuid);
    if (mapView != null) {
        try {//from  w  w  w . jav a  2 s.  co  m
            mapView.onResume();
        } catch (Throwable e) {
            FileLog.e("tmessages", e);
        }
    }
    if (googleMap != null) {
        try {
            googleMap.setMyLocationEnabled(true);
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
    }
    updateUserData();
    fixLayoutInternal(true);
    if (checkPermission && Build.VERSION.SDK_INT >= 23) {
        Activity activity = getParentActivity();
        if (activity != null) {
            checkPermission = false;
            if (activity.checkSelfPermission(
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                activity.requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION }, 2);
            }
        }
    }
}

From source file:org.artoolkit.ar.samples.nftBook.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {/*  ww  w.j  a va  2s. c om*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        nftBookActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:org.artoolkit.ar.samples.ARNativeES1.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {//from   ww  w. j a v a 2  s.c  om
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        ARNativeES1Activity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}