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:com.adafruit.bluefruit.le.connect.app.MainActivity.java

@TargetApi(Build.VERSION_CODES.M)
private void requestLocationPermissionIfNeeded() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Android M Permission check
        if (this.checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can scan for Bluetooth peripherals");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                            PERMISSION_REQUEST_FINE_LOCATION);
                }//from  w  w w.j  a  v a2 s .co  m
            });
            builder.show();
        }
    }
}

From source file:br.com.brolam.cloudvision.ui.NoteVisionActivity.java

/**
 * Callback for the result from requesting permissions. This method
 * is invoked for every call on {@link #requestPermissions(String[], int)}.
 * <p>/*from  w w w  .ja v a2 s. c  om*/
 * <strong>Note:</strong> It is possible that the permissions request interaction
 * with the user is interrupted. In this case you will receive empty permissions
 * and results arrays which should be treated as a cancellation.
 * </p>
 *
 * @param requestCode  The request code passed in {@link #requestPermissions(String[], int)}.
 * @param permissions  The requested permissions. Never null.
 * @param grantResults The grant results for the corresponding permissions
 *                     which is either {@link PackageManager#PERMISSION_GRANTED}
 *                     or {@link PackageManager#PERMISSION_DENIED}. Never null.
 * @see #requestPermissions(String[], int)
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode != ActivityHelper.RC_HANDLE_CAMERA_PERM) {
        Log.d(TAG, "Got unexpected permission result: " + requestCode);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        return;
    }

    if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "Camera permission granted - initialize the camera source");
        // We have permission, so create the camerasource
        createCameraSource();
        return;
    }

    Log.e(TAG, "Permission not granted: results len = " + grantResults.length + " Result code = "
            + (grantResults.length > 0 ? grantResults[0] : "(empty)"));

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            finish();
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.app_name).setMessage(R.string.no_camera_permission)
            .setPositiveButton(R.string.ok, listener).show();
}

From source file:fr.cph.chicago.util.Util.java

public static void setLocationOnMap(@NonNull final Activity activity, @NonNull final GoogleMap googleMap) {
    if (ActivityCompat.checkSelfPermission(activity,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(activity,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
        return;/*w w  w. ja  v a2s  .co  m*/
    }
    googleMap.setMyLocationEnabled(true);
}

From source file:de.tap.easy_xkcd.fragments.comics.ComicBrowserFragment.java

public void shareComicImage() {
    if (!(ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
        ActivityCompat.requestPermissions(getActivity(),
                new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
        return;/*from w ww  .jav  a  2s  .c om*/
    }
    new ShareImageTask().execute(comicMap.get(lastComicNumber).getComicData()[2]);
}

From source file:com.andrew.apollo.MusicPlaybackService.java

/**
 * {@inheritDoc}/* w w w .  j a  v  a  2s.c o m*/
 */
@Override
public void onCreate() {
    if (D)
        LOG.info("Creating service");
    super.onCreate();

    boolean permissionGranted = runStrict(() -> PackageManager.PERMISSION_GRANTED == ContextCompat
            .checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            && PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE));

    if (permissionGranted) {

        try {
            // "This initService() call may result in ANRs, when some of the ContentResovler queries (like getCardId())
            // take too long.
            //
            // I didn't want to use Engine.instance().getThreadPool() as this might be initialized before EngineService
            // give this is a service declared in AndroidManifest.xml"
            // -gubatron
            new Thread(this::initService).start();
        } catch (Throwable ignored) {
        }
    }
}

From source file:com.github.akinaru.hcidebugger.activity.HciDebuggerActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
    case REQUEST_PERMISSION_READ_EXTERNAL: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            bindService();//from w  w w .  j ava 2s  . c o  m
        } else {
            finish();
        }
        break;
    }
    case REQUEST_PERMISSION_COARSE_LOCATION: {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    startBleScan();
                    startScanSetup();
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(HciDebuggerActivity.this, "permission coarse location required for ble scan",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    }
}

From source file:it.unime.mobility4ckan.MainActivity.java

private void enableGPS() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getApplicationContext(), "Avvio GPS Fallito", Toast.LENGTH_LONG).show();
        return;/*from w ww. j  av  a2  s .  c om*/
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

From source file:com.alibaba.weex.extend.module.location.DefaultLocation.java

@Override
public void clearWatch(String watchId) {
    WXLogUtils.d("into--[clearWatch] mWatchId:" + watchId);
    if (mWXSDKInstance == null || mWXSDKInstance.isDestroy() || mLocationManager == null) {
        return;//from w  ww. ja  v  a 2s .c o  m
    }
    if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        WXLocationListener listener = mRegisterSucCallbacks.get(watchId);
        if (listener != null) {
            listener.destroy();
            mLocationManager.removeUpdates(listener);
        }
        mRegisterSucCallbacks.remove(watchId);
    }
}

From source file:com.dvn.vindecoder.ui.user.GetAllVehicalDetails.java

public static boolean checkPermission(String permission, Context context) {
    int statusCode = ContextCompat.checkSelfPermission(context, permission);
    return statusCode == PackageManager.PERMISSION_GRANTED;
}

From source file:com.amaze.filemanager.fragments.preference_fragments.Preffrag.java

public boolean checkGplusPermission() {
    // Verify that all required contact permissions have been granted.
    if (ActivityCompat.checkSelfPermission(getActivity(),
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }/* www  .ja  va2  s .com*/
    return true;
}