Java Code Examples for android.support.v4.app.ActivityCompat#requestPermissions()

The following examples show how to use android.support.v4.app.ActivityCompat#requestPermissions() . 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
Source File: Fido2DemoActivity.java    From security-samples with Apache License 2.0 6 votes vote down vote up
private void getRegisterRequest() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
            == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "getRegisterRequest permission is granted");

        Task<PublicKeyCredentialCreationOptions> getRegisterRequestTask = asyncGetRegisterRequest();
        getRegisterRequestTask.addOnCompleteListener(
                new OnCompleteListener<PublicKeyCredentialCreationOptions>() {
                    @Override
                    public void onComplete(@NonNull Task<PublicKeyCredentialCreationOptions> task) {
                        PublicKeyCredentialCreationOptions options = task.getResult();
                        if (options == null) {
                            Log.d(TAG, "Register request is null");
                            return;
                        }
                        sendRegisterRequestToClient(options);
                    }
                });
    } else {
        Log.i(TAG, "getRegisterRequest permission is requested");
        ActivityCompat.requestPermissions(
                this,
                new String[] {Manifest.permission.GET_ACCOUNTS},
                GET_ACCOUNTS_PERMISSIONS_REQUEST_REGISTER);
    }
}
 
Example 2
Source File: MainActivity.java    From dialogflow-java-client with Apache License 2.0 6 votes vote down vote up
protected void checkAudioRecordPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.RECORD_AUDIO)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.RECORD_AUDIO},
                    REQUEST_AUDIO_PERMISSIONS_ID);

        }
    }
}
 
Example 3
Source File: NewTodoActivity.java    From ToDoList with Apache License 2.0 6 votes vote down vote up
private void initPermission() {
    String permission[] = {Manifest.permission.RECORD_AUDIO,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    ArrayList<String> applyList = new ArrayList<>();

    for (String per : permission) {
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, per)) {
            applyList.add(per);
        }
    }

    String tmpList[] = new String[applyList.size()];
    if (!applyList.isEmpty()) {
        ActivityCompat.requestPermissions(this, applyList.toArray(tmpList), 123);
    }
}
 
Example 4
Source File: CallActivity.java    From restcomm-android-sdk with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean handlePermissions(boolean isVideo)
{
    ArrayList<String> permissions = new ArrayList<>(Arrays.asList(new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.USE_SIP}));
    if (isVideo) {
        // Only add CAMERA permission if this is a video call
        permissions.add(Manifest.permission.CAMERA);
    }

    if (!havePermissions(permissions)) {
        // Dynamic permissions where introduced in M
        // PERMISSION_REQUEST_DANGEROUS is an app-defined int constant. The callback method (i.e. onRequestPermissionsResult) gets the result of the request.
        ActivityCompat.requestPermissions(this, permissions.toArray(new String[permissions.size()]), PERMISSION_REQUEST_DANGEROUS);

        return false;
    }

    resumeCall();

    return true;
}
 
Example 5
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
private void connectHeartRateSensor() {
  int permission = ActivityCompat.checkSelfPermission(this,
    Manifest.permission.BODY_SENSORS);
  if (permission == PERMISSION_GRANTED) {
    // If permission granted, connect the event listener.
    doConnectHeartRateSensor();
  } else {
    if (ActivityCompat.shouldShowRequestPermissionRationale(
      this, Manifest.permission.BODY_SENSORS)) {
      // TODO: Display additional rationale for the requested permission.
    }

    // Request the permission
    ActivityCompat.requestPermissions(this,
      new String[]{Manifest.permission.BODY_SENSORS}, BODY_SENSOR_PERMISSION_REQUEST);
  }
}
 
Example 6
Source File: CreateWalletActivity.java    From Upchain-wallet with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    ImmersionBar.with(this)
            .titleBar(commonToolbar, false)
            .navigationBarColor(R.color.white)
            .init();


    boolean hasPermission = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_STORAGE);
    }

}
 
Example 7
Source File: MainActivity.java    From android-silent-ping-sms with GNU General Public License v3.0 6 votes vote down vote up
boolean checkPermissions() {
    List<String> missingPermissions = new ArrayList<>();

    int sendSmsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);
    int readPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
    int receiveSmsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);

    if (sendSmsPermission != PackageManager.PERMISSION_GRANTED) {
        missingPermissions.add(Manifest.permission.SEND_SMS);
    }

    if (readPhonePermission != PackageManager.PERMISSION_GRANTED) {
        missingPermissions.add(Manifest.permission.READ_PHONE_STATE);
    }

    if (receiveSmsPermission != PackageManager.PERMISSION_GRANTED && preferences.getBoolean(PREF_RECEIVE_DATA_SMS, false)) {
        missingPermissions.add(Manifest.permission.RECEIVE_SMS);
    }

    if (!missingPermissions.isEmpty()) {
        ActivityCompat.requestPermissions(this, missingPermissions.toArray(new String[0]), 1);
        return false;
    }

    return true;
}
 
Example 8
Source File: PermissionsUtil.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
public static boolean checkAndRequest(@NonNull final Activity activity, @NonNull final String permission, final int requestCode, int messagePermissionResId, DialogInterface.OnClickListener onCancelListener) {
    boolean result = false;
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder
                    .setMessage(messagePermissionResId)
                    .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
                }
            }).setNegativeButton(R.string.cancel_uc, onCancelListener).show();

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);

            // MY_PERMISSIONS_REQUEST is an app-defined int constant.
            // The callback method gets the result of the request.
        }
    } else {
        result = true;
    }
    return result;
}
 
Example 9
Source File: Preferences.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void checkReadPermission(final Activity activity) {

            // TODO call log permission - especially for Android 9+
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (xdrip.getAppContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(activity,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            Constants.GET_PHONE_READ_PERMISSION);
                }
            }
        }
 
Example 10
Source File: BarcodeCaptureActivity.java    From trust-wallet-android-source with GNU General Public License v3.0 5 votes vote down vote up
private void requestCameraPermission() {
    Log.w(TAG, "Camera permission is not granted. Requesting permission");

    final String[] permissions = new String[]{Manifest.permission.CAMERA};

    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
    }
}
 
Example 11
Source File: GalleryActivity.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
void startPhotoTaker() {

        int permissionCheck = ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA);

        if (permissionCheck ==PackageManager.PERMISSION_DENIED)
        {
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {

                View view = findViewById(R.id.gallery_fragment);

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                Snackbar.make(view, R.string.grant_perms, Snackbar.LENGTH_LONG).show();
            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
        else {

            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.SETTING_ONE_AND_DONE,false);
            startActivityForResult(intent, ConversationDetailActivity.REQUEST_TAKE_PICTURE);
        }
    }
 
Example 12
Source File: MainActivity.java    From RapidSphinx with MIT License 5 votes vote down vote up
private void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.RECORD_AUDIO},
                1);
    }
}
 
Example 13
Source File: AFPermissionUtil.java    From AFBaseLibrary with Apache License 2.0 5 votes vote down vote up
@TargetApi(23)
private static void executePermissionsRequest(Object object, String[] perms, int requestCode) {
    checkCallingObjectSuitability(object);

    if (object instanceof Activity) {
        ActivityCompat.requestPermissions((Activity) object, perms, requestCode);
    } else if (object instanceof Fragment) {
        ((Fragment) object).requestPermissions(perms, requestCode);
    } else if (object instanceof android.app.Fragment) {
        ((android.app.Fragment) object).requestPermissions(perms, requestCode);
    }
}
 
Example 14
Source File: SampleAct.java    From RxDownloader with MIT License 5 votes vote down vote up
private void checkPermission() {
    final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int permissionCheck = ContextCompat.checkSelfPermission(this, permission);

    if (permissionCheck != PermissionChecker.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{permission}, 0);
        return;
    }
    startSample();
}
 
Example 15
Source File: Fido2DemoActivity.java    From security-samples with Apache License 2.0 5 votes vote down vote up
private void updateAndDisplayRegisteredKeys() {
    progressBar.setVisibility(View.VISIBLE);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
            == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "updateAndDisplayRegisteredKeys permission is granted");
        Task<List<Map<String, String>>> refreshSecurityKeyTask = asyncRefreshSecurityKey();
        refreshSecurityKeyTask.addOnCompleteListener(
                new OnCompleteListener<List<Map<String, String>>>() {
                    @Override
                    public void onComplete(@NonNull Task<List<Map<String, String>>> task) {
                        List<Map<String, String>> tokens = task.getResult();
                        if (tokens == null) {
                            swipeRefreshLayout.setRefreshing(false);
                            progressBar.setVisibility(View.GONE);
                            return;
                        }
                        securityTokens = tokens;
                        adapter.clearSecurityTokens();
                        adapter.addSecurityToken(securityTokens);
                        displayRegisteredKeys();
                    }
                });
    } else {
        Log.i(TAG, "updateAndDisplayRegisteredKeys permission is requested");
        ActivityCompat.requestPermissions(
                this,
                new String[] {Manifest.permission.GET_ACCOUNTS},
                GET_ACCOUNTS_PERMISSIONS_ALL_TOKENS);
    }
}
 
Example 16
Source File: MainActivity.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public boolean isMicrophonePermissionGranted ()
{
    Boolean micPermission = selfPermissionGranted(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
    //
    if (!micPermission)
        ActivityCompat.requestPermissions(this, PERMISSIONS_MICROPHONE, REQUEST_MICROPHONE);
    //
    return micPermission;
}
 
Example 17
Source File: HomeActivity.java    From microbit with Apache License 2.0 4 votes vote down vote up
private void requestPermission(String[] permissions, final int requestCode) {
    ActivityCompat.requestPermissions(this, permissions, requestCode);
}
 
Example 18
Source File: AppLocationActivity.java    From GooglePlayServiceLocationSupport with Apache License 2.0 4 votes vote down vote up
@Override
public Location requestForCurrentLocation() {
    // If Google Play Services is available
    Location currentLocation = null;
    if (servicesConnected()) {
        // Get the current location
        currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    }
    if (currentLocation == null) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_LOCATION);
        else {
            Location locationGPS = mLocationService.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location locationNet = mLocationService.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            long GPSLocationTime = 0;
            if (null != locationGPS) {
                GPSLocationTime = locationGPS.getTime();
            }

            long NetLocationTime = 0;

            if (null != locationNet) {
                NetLocationTime = locationNet.getTime();
            }

            if (0 < GPSLocationTime - NetLocationTime) {
                currentLocation = locationGPS;
            } else {
                currentLocation = locationNet;
            }
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION);
        else
            currentLocation = mLocationService.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION);
        else
            currentLocation = mLocationService.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    if (currentLocation != null)
        myCurrentLocation(currentLocation);
    return currentLocation;
}
 
Example 19
Source File: Permission.java    From soundcom with Apache License 2.0 4 votes vote down vote up
protected void makeRecordRequest() {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.RECORD_AUDIO},
            REQUEST_WRITE_STORAGE);
}
 
Example 20
Source File: DevicePermissionHelper.java    From hr with GNU Affero General Public License v3.0 4 votes vote down vote up
public void requestPermissions(PermissionGrantListener callback, String[] permissions) {
    mPermissionGrantListener = callback;
    ActivityCompat.requestPermissions(mActivity, permissions, REQUEST_PERMISSIONS);
}