Java Code Examples for android.support.v4.content.ContextCompat.checkSelfPermission()

The following are Jave code examples for showing how to use checkSelfPermission() of the android.support.v4.content.ContextCompat 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: Android-Code-Demos   File: RequestPermissions.java   View Source Code Vote up 7 votes
public static void requestRuntimePermission(Activity activity, String[] permissions,
                                            OnRequestPermissionsListener listener) {
    Activity topActivity = activity;
    if (null == topActivity) {
        return;
    }
    mListener = listener;
    List<String> permissionList = new ArrayList<>();
    for (String permission : permissions) {
        if (ContextCompat.checkSelfPermission(topActivity, permission)
                != PackageManager.PERMISSION_GRANTED) {

            permissionList.add(permission);
        }
    }
    if (!permissionList.isEmpty()) {
        ActivityCompat.requestPermissions(topActivity,
                permissionList.toArray(new String[permissionList.size()]),
                PERMISSION_REQUEST_CODE);
    } else {
        mListener.onGranted();
    }
}
 
Example 2
Project: Tribe   File: DraggablePresenterImpl.java   View Source Code Vote up 7 votes
/**
 * 拍照
 */
private void takePhoto() {
    //在每次拍照做权限的判断,防止出现权限不全而获取不到图像
    if (ContextCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(activity,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
        photoUri = get24MediaFileUri(TYPE_TAKE_PHOTO);
        takeIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }else {
        photoUri = getMediaFileUri(TYPE_TAKE_PHOTO);
    }
    takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    activity.startActivityForResult(takeIntent, CODE_TAKE_PHOTO);
}
 
Example 3
Project: Clipcon-AndroidClient   File: TransparentActivity.java   View Source Code Vote up 7 votes
private void getPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

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

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
            // 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.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    } else {
    }
}
 
Example 4
Project: MobileAppForPatient   File: AllDeviceListActivity.java   View Source Code Vote up 6 votes
/**
 * connect to the bluetooth
 *
 * @param deviceInfo
 */
private void connectToBluetooth(final DeviceInfoBean deviceInfo) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            switch (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
                case PackageManager.PERMISSION_DENIED:
                    performPermissionDeniedAction(deviceInfo);
                    break;
                case PackageManager.PERMISSION_GRANTED:
                    startDeviceConnection(deviceInfo, healthCareReceiver);
                    break;
                default:
                    break;
            }
        } else {
            startDeviceConnection(deviceInfo, healthCareReceiver);
        }

    } catch (RuntimeException e) {
        showToastMessage(e.getMessage(), mContext);
        Log.e(AppUtility.TAG, Log.getStackTraceString(e));
    }

}
 
Example 5
Project: Navalny.Pulse   File: RegisterActivity.java   View Source Code Vote up 6 votes
@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

if (!SharedPreferenceHelper.getToken().isEmpty()) {
	Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
	startActivity(intent);
	finish();
} else {
	setContentView(R.layout.activity_register);
	ButterKnife.bind(this);

	FormatWatcher formatWatcher = new MaskFormatWatcher(
			MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER) // маска для серии и номера
	);
	formatWatcher.installOn(phoneEditText);

	if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
		ActivityCompat.requestPermissions(this,
				new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
	}
}
  }
 
Example 6
Project: KudanVRDemo   File: MainActivity.java   View Source Code Vote up 6 votes
public void permissionsRequest() {

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.INTERNET)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED ||ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 111);

        }
    }
 
Example 7
Project: tumbviewer   File: SystemDownload.java   View Source Code Vote up 6 votes
public static long downloadVideo(Context context, String url) {
    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        EventBus.getDefault().post(new EventPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE));
        return -1;
    }
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    if (url != null) {
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        File dst = FileUtils.createVideoFile(uri);
        if (dst.exists()) {
            Toast.makeText(context, R.string.video_exist, Toast.LENGTH_SHORT).show();
        } else {
            request.setDestinationUri(Uri.fromFile(dst));
            request.allowScanningByMediaScanner();
            long id = manager.enqueue(request);
            Log.d("download id", "-->" + id);
            return id;
        }
    }
    return -1;
}
 
Example 8
Project: QuranAndroid   File: MainActivity.java   View Source Code Vote up 6 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splashscreen_main);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    //marshmallow check permission permissions
    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);
    } else {
        validateFilesAndDownload();
    }

}
 
Example 9
Project: BookyMcBookface   File: BookListActivity.java   View Source Code Vote up 5 votes
private boolean checkStorageAccess(boolean yay) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                yay? REQUEST_READ_EXTERNAL_STORAGE : REQUEST_READ_EXTERNAL_STORAGE_NOYAY);
        return false;
    }
    return true;
}
 
Example 10
Project: Phoenix-for-VK   File: ChatPrensenter.java   View Source Code Vote up 5 votes
private boolean hasAudioRecordPermissions() {
    final Context app = getApplicationContext();

    int recordPermission = ContextCompat.checkSelfPermission(app, Manifest.permission.RECORD_AUDIO);
    int writePermission = ContextCompat.checkSelfPermission(app, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    return recordPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED;
}
 
Example 11
Project: LBSTest   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLocationClient = new LocationClient(getApplicationContext());
    mLocationClient.registerLocationListener(new MyLocationListener());
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    mapView = (MapView) findViewById(R.id.bmapView);
    baiduMap = mapView.getMap();
    baiduMap.setMyLocationEnabled(true);
    positionText = (TextView) findViewById(R.id.position_text_view);
    List<String> permissionList = new ArrayList<>();
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }

    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
        permissionList.add(Manifest.permission.READ_PHONE_STATE);
    }

    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    if (!permissionList.isEmpty()){
        String [] permissions = permissionList.toArray(new String[permissionList.size()]);
        ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
    }else {
        requestLocation();
    }
}
 
Example 12
Project: LuaViewPlayground   File: ScanBridge.java   View Source Code Vote up 5 votes
/**
 * 用于启动扫码的Activity,启动之前要做权限判断。
 */
@LuaViewApi
public void start() {
    if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {//还没有授予权限
        if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CAMERA)) {
            Toast.makeText(mActivity, "Please Allow the Camera permission.", Toast.LENGTH_SHORT).show();
        } else {
            ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, MainActivity.REQUEST_CAMERA);
        }
    } else {
        mActivity.startActivityForResult(new Intent(mActivity , CaptureActivity.class) , 0);
    }
}
 
Example 13
Project: GitHub   File: DraweeMediaPickerFragment.java   View Source Code Vote up 5 votes
private boolean ensureExternalStoragePermissionGranted() {
  if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(
        getActivity(),
        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
        PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
    return false;
  }
  return true;
}
 
Example 14
Project: ucar-weex-core   File: SinglePermission.java   View Source Code Vote up 5 votes
private String[] notHasPermission(Context context, String[] permissions) {
    ArrayList<String> notPermissions = new ArrayList<>();
    for (String permission : permissions) {
        try {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                notPermissions.add(permission);
            }
        } catch (Throwable t) {
            Log.e(String.format("Failure checking permission %s", permission), t.getMessage());
        }
    }
    return notPermissions.toArray(new String[]{});
}
 
Example 15
Project: Movie-Notifier-Android   File: LocationUtil.java   View Source Code Vote up 5 votes
public void setupLocationClient(Context context) {
    if(locationClient == null) {
        if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationClient = LocationServices.getFusedLocationProviderClient(context);
        }
    }
}
 
Example 16
Project: AlarmWithL-T   File: FormActivity.java   View Source Code Vote up 5 votes
private void checkPermission() {
    Log.d("onCreate", "checkPermission");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION);
        }
    }

}
 
Example 17
Project: kotlin-android   File: DBR.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    //apply for camera permission
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=  PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
        }
        else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
        }
    }

    mPreview = (FrameLayout) findViewById(R.id.camera_preview);
    mFlashImageView = (ImageView)findViewById(R.id.ivFlash);
    mFlashTextView = (TextView)findViewById(R.id.tvFlash);
    mRectLayer = (RectLayer)findViewById(R.id.rectLayer);

    mSurfaceHolder = new CameraPreview(DBR.this);
    mPreview.addView(mSurfaceHolder);

    String license = "";

    Intent intent = getIntent();
    if (intent.getAction().equals(ACTION_BARCODE)) {
        mIsIntent = true;
        license = intent.getStringExtra("license");
    }

    mBarcodeReader = new BarcodeReader(license);
}
 
Example 18
Project: quiz_helper   File: AnkiDroidHelper.java   View Source Code Vote up 5 votes
/**
 * Whether or not we should request full access to the AnkiDroid API
 */
public boolean shouldRequestPermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return false;
    }
    return ContextCompat.checkSelfPermission(mContext, READ_WRITE_PERMISSION) != PackageManager.PERMISSION_GRANTED;
}
 
Example 19
Project: GitHub   File: MainActivity.java   View Source Code Vote up 4 votes
private boolean hasPermission() {
  return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
      == PackageManager.PERMISSION_GRANTED;
}
 
Example 20
Project: UDOOBluLib-android   File: UdooBluService.java   View Source Code Vote up 4 votes
private boolean isPermissionGranted(String permission, Context context) {
    return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}