Java Code Examples for androidx.core.app.ActivityCompat#requestPermissions()

The following examples show how to use androidx.core.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: DriverHome.java    From UberClone with MIT License 6 votes vote down vote up
private void setUpLocation() {
    if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION
        }, REQUEST_CODE_PERMISSION);
    }else{
        if (checkPlayServices()){
            buildGoogleApiClient();
            if (locationSwitch.isChecked()){
                drivers= FirebaseDatabase.getInstance().getReference(Common.driver_tbl).child(Common.currentUser.getCarType());
                geoFire=new GeoFire(drivers);
                displayLocation();
            }
        }
    }
}
 
Example 2
Source File: MainActivity.java    From ssj with GNU General Public License v3.0 6 votes vote down vote up
/**
 *
 */
private void checkPermissions()
{
	if (Build.VERSION.SDK_INT >= 23)
	{
		//dangerous permissions
		if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
				!= PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(this, Manifest.permission.BODY_SENSORS)
				!= PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
				!= PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
				!= PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
				!= PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(this, new String[]{
					Manifest.permission.ACCESS_FINE_LOCATION,
					Manifest.permission.BODY_SENSORS,
					Manifest.permission.CAMERA,
					Manifest.permission.RECORD_AUDIO,
					Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_DANGEROUS_PERMISSIONS);
		}
	}
}
 
Example 3
Source File: ConfigActivity.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dt = new DataHelper(this);

    ep = getSharedPreferences("Settings", 0);

    invalidateOptionsMenu();
    loadScreen();

    // request permissions
    ActivityCompat.requestPermissions(this, Constants.permissions, Constants.PERM_REQ);
    createDirs();

    if (ep.getInt("UpdateVersion", -1) < Utils.getVersion(this)) {
        ep.edit().putInt("UpdateVersion", Utils.getVersion(this)).apply();
        showChangelog(true, false);
    }

    checkIntent();
}
 
Example 4
Source File: AirMapMapView.java    From AirMapSDK-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void onDidFinishLoadingStyle() {
    // add my location w/ permission check
    LocationComponent locationComponent = map.getLocationComponent();
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((Activity) getContext(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MyLocationMapActivity.REQUEST_LOCATION_PERMISSION);
        return;
    }
    LocationComponentOptions options = LocationComponentOptions.builder(getContext())
            .elevation(2f)
            .accuracyAlpha(0f)
            .enableStaleState(false)
            .build();

    locationComponent.activateLocationComponent(getContext(), map.getStyle(), options);
    locationComponent.setLocationComponentEnabled(true);
}
 
Example 5
Source File: PermissionRequestHelper.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
public static void checkPermissionReadStorage(Activity activity) {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

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

            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    Constants.REQUEST_STORAGE_PERMISSION);

        } else {

            //Read external storage permissions have not been granted yet so request them directly
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    Constants.REQUEST_STORAGE_PERMISSION);
        }
    }
}
 
Example 6
Source File: EventInitialPresenter.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onLocationClick() {
    if (ActivityCompat.checkSelfPermission(view.getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(view.getAbstractActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            // TODO CRIS: 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 {
            ActivityCompat.requestPermissions(view.getAbstractActivity(),
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    ACCESS_LOCATION_PERMISSION_REQUEST);
        }
        return;
    }
    mFusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
        /*
         * if (location != null)
         * view.setLocation(GeometryHelper.createPointGeometry(location.getLatitude(),
         * location.getLongitude()));
         */
    });
}
 
Example 7
Source File: BiometricLoginUtils.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
private static boolean fingerprintPermissionGranted(@NonNull Activity activity) {
    Context context = getApplicationContext();
    boolean granted = false;

    // The protection level of USE_FINGERPRINT permission (added API 23) is normal instead of dangerous.
    // http://developer.android.com/reference/android/Manifest.permission.html#USE_FINGERPRINT
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

        if (!(ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) ==
                PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{android.Manifest.permission.USE_FINGERPRINT},
                    GlobalSetting.PERMISSION_USE_FINGERPRINT);

            granted = true;
        } else {
            granted = true;
        }
    }
    return granted;
}
 
Example 8
Source File: ExternalStoragePermissions.java    From InviZible with GNU General Public License v3.0 6 votes vote down vote up
public void requestReadWritePermissions() {
    // Storage Permissions variables
    final int REQUEST_EXTERNAL_STORAGE = 1;
    String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

    if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}
 
Example 9
Source File: LocationPermissionRequestActivity.java    From android-WifiRttScan with Apache License 2.0 5 votes vote down vote up
public void onClickApprovePermissionRequest(View view) {
    Log.d(TAG, "onClickApprovePermissionRequest()");

    // On 23+ (M+) devices, fine location permission not granted. Request permission.
    ActivityCompat.requestPermissions(
            this,
            new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
            PERMISSION_REQUEST_FINE_LOCATION);
}
 
Example 10
Source File: ActivityStateListener.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Request permissions from the system
 *
 * @param listener    Callback
 * @param permissions list of permissions
 */
public void request(ActivityPermissionListener listener, Collection<String> permissions) {
    if (listener != null) {
        autoPermissionListener.add(listener);
        int reqCode = getNextId();
        perms.put(listener, reqCode);
        ActivityCompat.requestPermissions(MainActivity.current(), permissions.toArray(new String[0]), reqCode);
    }
}
 
Example 11
Source File: BaseActivity.java    From PhotoEditor with MIT License 5 votes vote down vote up
public boolean requestPermission(String permission) {
    boolean isGranted = ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    if (!isGranted) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{permission},
                READ_WRITE_STORAGE);
    }
    return isGranted;
}
 
Example 12
Source File: OpenFilePlugin.java    From open_file with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.M)
private void openApkFile() {
    if (!canInstallApk()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startInstallPermissionSettingActivity();
        } else {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, REQUEST_CODE);
        }
    } else {
        startActivity();
    }
}
 
Example 13
Source File: SimpleVideoPlayer.java    From xGetter with Apache License 2.0 5 votes vote down vote up
private boolean checkPermissions() {
    int storage = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    final List<String> listPermissionsNeeded = new ArrayList<>();
    if (storage != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 1000);
        return false;
    }
    return true;
}
 
Example 14
Source File: PermissionUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * 第二次检查权限,用在某个操作需要某个权限的时候调用
 * @param context
 * @param requestCode   请求码
 * @param permission    权限数组
 * @return
 */
public static boolean checkPermissionSecond (Context context , int requestCode, String[] permission){

    List<String> permissions = new ArrayList<String>();
    for (String per : permission) {
        int permissionCode = ActivityCompat.checkSelfPermission(context, per );
        if( permissionCode != PackageManager.PERMISSION_GRANTED ) {
            permissions.add(per);
        }
    }
    if(!permissions.isEmpty()) {
        ActivityCompat.requestPermissions((Activity) context,permissions.toArray( new String[permissions.size()] ), requestCode);

        /*跳转到应用详情,让用户去打开权限*/
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(localIntent);
        return  false;
    }else{
        return  true;
    }
}
 
Example 15
Source File: LocationDisplayActivity.java    From NaviBee with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_display);

    lat = getIntent().getDoubleExtra(NavigationSelectorActivity.EXTRA_LATITUDE, 0.0);
    lon = getIntent().getDoubleExtra(NavigationSelectorActivity.EXTRA_LONGITUDE, 0.0);
    time = (Date) getIntent().getSerializableExtra(EXTRA_TIME);

    if (getIntent().hasExtra(EXTRA_SENDER))
        user = getIntent().getParcelableExtra(EXTRA_SENDER);

    mapView = findViewById(R.id.displayLocation_mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_FINE_LOCATION);
    }

    TextView title = findViewById(R.id.chat_locationDisplay_title);
    TextView subtitle = findViewById(R.id.chat_locationDisplay_subtitle);
    ImageView icon = findViewById(R.id.chat_locationDisplay_icon);

    CharSequence timeStr = DateUtils.getRelativeTimeSpanString(time.getTime());

    if (user != null) {
        title.setText(user.getName());
        subtitle.setText(timeStr);
        new URLImageViewCacheLoader(user.getPhotoUrl(), icon).roundImage(true).execute();

    } else {
        title.setText(timeStr);
        subtitle.setVisibility(View.GONE);
        icon.setVisibility(View.GONE);
    }
}
 
Example 16
Source File: CurrentPlaceTestActivity.java    From android-places-demos with Apache License 2.0 5 votes vote down vote up
private boolean checkPermission(String permission) {
  boolean hasPermission =
      ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
  if (!hasPermission) {
    ActivityCompat.requestPermissions(this, new String[]{permission}, 0);
  }
  return hasPermission;
}
 
Example 17
Source File: ExternalStorageHandler.java    From Lunary-Ethereum-Wallet with GNU General Public License v3.0 4 votes vote down vote up
public static void askForPermission(Activity c) {
    if (Build.VERSION.SDK_INT < 23) return;
    ActivityCompat.requestPermissions(c, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
}
 
Example 18
Source File: FileActivity.java    From lrkFM with MIT License 4 votes vote down vote up
/**
 * Checks if permission to access files is granted.
 *
 * @param context the context
 */
public static void verifyStoragePermissions(Activity context) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(context, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    }
}
 
Example 19
Source File: ExternalStorageHandler.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public static void askForPermission(Activity c) {
    if (Build.VERSION.SDK_INT < 23)
        return;
    ActivityCompat.requestPermissions(c, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
}
 
Example 20
Source File: MainActivity.java    From SmsMatrix with GNU General Public License v3.0 4 votes vote down vote up
private void askPermissions() {
    ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, PERMISSION_REQUEST_CODE);
}