Java Code Examples for android.content.pm.PackageManager.PERMISSION_GRANTED

The following are Jave code examples for showing how to use PERMISSION_GRANTED of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Paper-Melody   File: MainActivity.java   View Source Code Vote up 6 votes
/**
 * 用户权限申请的回调方法
 *
 * @param requestCode  请求码
 * @param permissions  请求的所有权限
 * @param grantResults 权限的结果
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_PERMISSION) {
        if (Build.VERSION.SDK_INT >= 23) {
            for (int i = 0; i < grantResults.length; ++i) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
                    boolean b = shouldShowRequestPermissionRationale(permissions[i]);
                    if (!b) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        showDialogTipUserGoToAppSetting();
                    } else
                        finish();
                }
            }
        }
    }
}
 
Example 2
Project: text_converter   File: BarCodeCodecFragment.java   View Source Code Vote up 6 votes
private void decodeImage() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        int result = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE);
        if (result != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getContext(), R.string.read_permission_msg, Toast.LENGTH_SHORT).show();
            String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE};
            requestPermissions(permissions, REQUEST_READ_EXTERNAL_STORAGE);
            return;
        }
    }

    try {
        Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
        getIntent.setType("image/*");

        Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        pickIntent.setType("image/*");

        Intent chooserIntent = Intent.createChooser(getIntent, getString(R.string.select_image));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});

        startActivityForResult(chooserIntent, REQUEST_PICK_IMAGE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Project: smart_commuter   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Check Permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET},10);
        }
    }
}
 
Example 4
Project: pullstring-android   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 0:
            // disable speech button is permission not granted or instantiate recorder
            if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                mRecordButton.setEnabled(false);
            } else {
                mSpeechRecorder = new SpeechRecorder(mConversation);
            }
            break;
        default:
            break;
    }
}
 
Example 5
Project: FlickLauncher   File: FingerprintHandler.java   View Source Code Vote up 5 votes
@TargetApi(23)
public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
    CancellationSignal cancellationSignal = new CancellationSignal();
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}
 
Example 6
Project: CXJPadProject   File: EasyPermissions.java   View Source Code Vote up 5 votes
private static void notifyAlreadyHasPermissions(Object object,
                                                int requestCode,
                                                @NonNull String[] perms) {
    int[] grantResults = new int[perms.length];
    for (int i = 0; i < perms.length; i++) {
        grantResults[i] = PackageManager.PERMISSION_GRANTED;
    }

    onRequestPermissionsResult(requestCode, perms, grantResults, object);
}
 
Example 7
Project: YZxing   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
public void onClick(View v) {
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        goScanner();
    } else {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISION_CODE_CAMARE);
    }
}
 
Example 8
Project: chromium-swe-updater   File: MainActivity.java   View Source Code Vote up 5 votes
/**Checks for the WRITE permission on the external storage. If not present it asks for it*/
private void checkPermissions() {
    int canRead =  ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if(canRead != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_WRITE);
}
 
Example 9
Project: DinningShare   File: CordovaInterfaceImpl.java   View Source Code Vote up 5 votes
public boolean hasPermission(String permission)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        int result = activity.checkSelfPermission(permission);
        return PackageManager.PERMISSION_GRANTED == result;
    }
    else
    {
        return true;
    }
}
 
Example 10
Project: GracefulMovies   File: LocationService.java   View Source Code Vote up 5 votes
private void initLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);

    String locationProvider;
    /**
     * 如果首选GPS定位,会存在这种情况,上次GPS启动采集数据在A地,本次在B地需要定位,但用户恰好在室内无
     * GPS信号,只好使用上次定位数据,就出现了地区级偏差。而网络定位则更具有实时性,在精度要求不高以及室内
     * 使用场景更多的前提下,首选网络定位
     */
    if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        locationProvider = LocationManager.NETWORK_PROVIDER; // 首选网络定位
    } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
        locationProvider = LocationManager.GPS_PROVIDER;
    } else {
        locationProvider = LocationManager.PASSIVE_PROVIDER;
    }

    if (mLocationListener != null)
        mLocationManager.requestLocationUpdates(locationProvider, 2000, 10, mLocationListener);
}
 
Example 11
Project: FloatingNew   File: FeedbackActivity.java   View Source Code Vote up 5 votes
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {

        case REQUEST_PERMISSIONS:

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                selectPicture();

            } else {
                // Permission Denied
                showMessageOKCancel("You need to allow access to SD card to select images.",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                goToSettings();

                            }
                        });

            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
 
Example 12
Project: AndiCar   File: FileListActivity.java   View Source Code Vote up 5 votes
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        fillFileList();
    }
    else {
        Toast.makeText(this, R.string.error_070, Toast.LENGTH_LONG).show();
        finish();
    }
}
 
Example 13
Project: OTAUpdates   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(getString(R.string.app_name), "Permission granted. Files can be saved");
            } else {
                Log.e(getString(R.string.app_name), "Permission denied. The App won\'t work");
                finish();
            }
            break;
    }
}
 
Example 14
Project: KrGallery   File: GalleryActivity.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
    if (savedInstanceState == null) {
        Intent intent = getIntent();
        config = intent.getParcelableExtra(GALLERY_CONFIG);
    } else {
        config = savedInstanceState.getParcelable(GALLERY_CONFIG);
    }
    Gallery.init(getApplication());
    FrameLayout mian = (FrameLayout) findViewById(R.id.mian);
    actionBarLayout = new ActionBarLayout(this);
    mian.addView(actionBarLayout);
    actionBarLayout.init(mainFragmentsStack);
    actionBarLayout.setDelegate(this);
    String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";
    if (checkCallingOrSelfPermission(
            READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(new String[]{
                    READ_EXTERNAL_STORAGE
            }, 1);
            return;
        }
    }
    showContent(config);
}
 
Example 15
Project: Runnest   File: ChallengeActivity.java   View Source Code Vote up 5 votes
/**
 * Handle request permissions result. Update what is needed and give a feedback to the user.
 *
 * @param requestCode       Code of the request.
 * @param permissions       Requested permissions.
 * @param grantResults      Result of the request.
 */
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE_FINE_LOCATION:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), R.string.toast_start_challenge,Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), R.string.toast_start_challenge_denied,
                        Toast.LENGTH_LONG).show();
            }
            break;
    }
}
 
Example 16
Project: OCR-Reader   File: OcrCaptureActivity.java   View Source Code Vote up 5 votes
/**
 * Initializes the UI and creates the detector pipeline.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.ocr_capture);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay);

    // read parameters from the intent used to launch the activity.
    boolean autoFocus = getIntent().getBooleanExtra(AutoFocus, false);
    boolean useFlash = getIntent().getBooleanExtra(UseFlash, false);

    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource(autoFocus, useFlash);
    } else {
        requestCameraPermission();
    }

    gestureDetector = new GestureDetector(this, new CaptureGestureListener());
    scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());

    Snackbar.make(mGraphicOverlay, "Tap to capture. Pinch/Stretch to zoom",
            Snackbar.LENGTH_INDEFINITE)
            .show();
}
 
Example 17
Project: AudioRecordDemo   File: EasyPermissions.java   View Source Code Vote up 5 votes
/**
 * Handle the result of a permission request, should be called from the calling Activity's {@link android.support.v4.app.ActivityCompat
 * .OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])} method.
 * <p>
 * If any permissions were granted or denied, the {@code object} will receive the appropriate callbacks through {@link PermissionCallbacks} and
 * methods annotated with {@link AfterPermissionGranted} will be run if appropriate.
 *
 * @param requestCode
 *         requestCode argument to permission result callback.
 * @param permissions
 *         permissions argument to permission result callback.
 * @param grantResults
 *         grantResults argument to permission result callback.
 * @param receivers
 *         an array of objects that have a method annotated with {@link AfterPermissionGranted} or implement {@link PermissionCallbacks}.
 */
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                              @NonNull int[] grantResults,
                                              @NonNull Object... receivers) {

    // Make a collection of granted and denied permissions from the request.
    ArrayList<String> granted = new ArrayList<String>();
    ArrayList<String> denied = new ArrayList<String>();
    for (int i = 0; i < permissions.length; i++) {
        String perm = permissions[i];
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
            granted.add(perm);
        } else {
            denied.add(perm);
        }
    }

    // iterate through all receivers
    for (Object object : receivers) {
        // Report granted permissions, if any.
        if (!granted.isEmpty()) {
            if (object instanceof PermissionCallbacks) {
                ((PermissionCallbacks) object).onPermissionsGranted(requestCode, granted);
            }
        }

        // Report denied permissions, if any.
        if (!denied.isEmpty()) {
            if (object instanceof PermissionCallbacks) {
                ((PermissionCallbacks) object).onPermissionsDenied(requestCode, denied);
            }
        }
    }
}
 
Example 18
Project: GPSTracker   File: Route.java   View Source Code Vote up 5 votes
@Override
public void onConnected(Bundle connectionHint) {
        startLocationUpdates();
    if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
    }

}
 
Example 19
Project: Paprika   File: BaseFragment.java   View Source Code Vote up 5 votes
@TargetApi(Build.VERSION_CODES.M)
public boolean hasPermissions(String[] permissions) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return true;
    for (String permission : permissions) {
        if (ContextCompat.checkSelfPermission(getBaseActivity(), permission) != PackageManager.PERMISSION_GRANTED)
            return false;
    }
    return true;
}
 
Example 20
Project: chromium-for-android-56-debug-video   File: Utils.java   View Source Code Vote up 4 votes
public static boolean isBluetoothPermissionGranted() {
    return PermissionChecker.checkSelfPermission(
            ContextUtils.getApplicationContext(), Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED;
}