Example usage for android.content.pm PackageManager PERMISSION_GRANTED

List of usage examples for android.content.pm PackageManager PERMISSION_GRANTED

Introduction

In this page you can find the example usage for android.content.pm PackageManager PERMISSION_GRANTED.

Prototype

int PERMISSION_GRANTED

To view the source code for android.content.pm PackageManager PERMISSION_GRANTED.

Click Source Link

Document

Permission check result: this is returned by #checkPermission if the permission has been granted to the given package.

Usage

From source file:Main.java

@SuppressWarnings("MissingPermission")
public static String[] getGoogleAccounts(Context ctx) {
    if (ctx.checkCallingOrSelfPermission(
            Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
        Account[] accounts = AccountManager.get(ctx).getAccountsByType("com.google");
        String[] result = new String[accounts.length];
        for (int i = 0; i < accounts.length; i++) {
            result[i] = accounts[i].name;
        }/*from ww  w .  j  a  v  a2 s. c om*/
        return result;
    }
    return null;
}

From source file:Main.java

static void showPermissionStatus(TextView tvOutput, String[] permissions, int[] grantResults) {
    StringBuilder stringBuilder = new StringBuilder();

    for (int i = 0; i < permissions.length; i++) {
        String permission = permissions[i];
        boolean granted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
        if (granted) {
            stringBuilder.append(permission).append(" was granted").append("\n");
        } else {/*w  w  w.j a v  a 2  s  . co m*/
            stringBuilder.append(permission).append(" wasn't granted").append("\n");
        }
    }

    Log.d("rx_permissions_result", stringBuilder.toString());
    tvOutput.setText(stringBuilder.toString());
}

From source file:Main.java

@SuppressWarnings("MissingPermission")
public static String getBluetoothMAC(Context context) {
    String result = null;/* w  w  w  . j  av  a 2s  .  com*/
    try {
        if (context.checkCallingOrSelfPermission(
                Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
            BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
            result = bta.getAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static boolean isHasPermission(Context context, String name) {
    try {/*w  ww . ja  v a2  s.co  m*/
        return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(name,
                context.getPackageName());
    } catch (Exception e) {
        // TODO: handle exception
    }
    return false;
}

From source file:Main.java

public static boolean isHavePermission(Context context, String name) {
    try {/* w  w w.ja v a 2 s  . c om*/
        return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(name,
                context.getPackageName());
    } catch (Exception e) {
        // TODO: handle exception
    }
    return false;
}

From source file:Main.java

public static boolean hasPermission(Context context, String permission) {
    return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

From source file:Main.java

/**
 * Checks if the result contains a {@link PackageManager#PERMISSION_GRANTED} result for a
 * permission from a runtime permissions request.
 *
 * @see android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback
 *//*from  w ww  . j  a va2 s . c o m*/
public static boolean isPermissionGranted(String[] grantPermissions, int[] grantResults, String permission) {
    for (int i = 0; i < grantPermissions.length; i++) {
        if (permission.equals(grantPermissions[i])) {
            return grantResults[i] == PackageManager.PERMISSION_GRANTED;
        }
    }
    return false;
}

From source file:Main.java

/**Check whether the app has been granted permission
 * @param context The context of your app
 * @param permission The string of permission which you want to check
 * @return True if the permission has been granted, otherwise return false
 */// w  w  w.j  a  v  a  2 s .  co  m
public static boolean isGrantedPermission(Context context, String permission) {
    if (TextUtils.isEmpty(permission)) {
        return false;
    }
    String packageName = context.getPackageName();
    return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(permission,
            packageName);
}

From source file:Main.java

/**
 * Check that all given permissions have been granted by verifying that each entry in the
 * given array is of the value {@link PackageManager#PERMISSION_GRANTED}.
 *
 * @see Activity#onRequestPermissionsResult(int, String[], int[])
 *///w  w w.j av a  2s  .co  m
public static boolean verifyPermissions(int[] grantResults) {
    // At least one result must be checked.
    if (grantResults.length < 1) {
        return false;
    }

    // Verify that each required permission has been granted, otherwise return false.
    for (int result : grantResults) {
        if (result != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Find out if we have the permission to access K-9 Mail's content provider.
 *
 * @param context/*ww w . j a v a2 s.  c om*/
 *         Used to retrieve the package manager.
 *
 * @return {@code true} if we have the needed permission, {@code false} otherwise.
 */
public static final boolean hasK9ReadPermission(Context context) {
    PackageManager manager = context.getPackageManager();
    try {
        return (manager.checkPermission(PERMISSION,
                context.getPackageName()) == PackageManager.PERMISSION_GRANTED);
    } catch (Exception e) {
        return false;
    }
}