android.support.v4.app.ActivityCompat Java Examples
The following examples show how to use
android.support.v4.app.ActivityCompat.
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: Flashlight.java From always-on-amoled with GNU General Public License v3.0 | 6 votes |
public Flashlight(Context context) { this.context = context; if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { try { cam = Camera.open(); Camera.Parameters p = cam.getParameters(); p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); cam.setParameters(p); SurfaceTexture mPreviewTexture = new SurfaceTexture(0); try { cam.setPreviewTexture(mPreviewTexture); } catch (IOException ignored) { } } catch (RuntimeException e) { Utils.showErrorNotification(context, context.getString(R.string.error), context.getString(R.string.error_5_camera_cant_connect_desc), 233, null); } } }
Example #2
Source File: CombineFenceApiActivity.java From android-samples with Apache License 2.0 | 6 votes |
@Override public void onClick(final View view) { switch (view.getId()) { case R.id.register_fence: //Check for the location permission. We need them to generate location fence. if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions( this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); } else { registerFence(); } break; case R.id.unregister_fence: unregisterFence(); break; } }
Example #3
Source File: QRScanActivity.java From SoloPi with Apache License 2.0 | 6 votes |
private void requestCameraPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { Snackbar.make(mainLayout, R.string.qr__camera_permission, Snackbar.LENGTH_INDEFINITE).setAction(R.string.constant__yes, new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions(QRScanActivity.this, new String[]{ Manifest.permission.CAMERA }, MY_PERMISSION_REQUEST_CAMERA); } }).show(); } else { Snackbar.make(mainLayout, R.string.qr__requst_permission, Snackbar.LENGTH_SHORT).show(); ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CAMERA }, MY_PERMISSION_REQUEST_CAMERA); } }
Example #4
Source File: BaseActivity.java From PainlessMusicPlayer with Apache License 2.0 | 6 votes |
@Override public boolean navigateUpTo(final Intent upIntent) { ComponentName destInfo = upIntent.getComponent(); if (destInfo == null) { destInfo = upIntent.resolveActivity(getPackageManager()); if (destInfo == null) { return false; } } if (shouldUpRecreateTask(upIntent)) { startActivity(upIntent); finish(); } else { ActivityCompat.finishAfterTransition(this); } return true; }
Example #5
Source File: JokeImageListRecyclerAdapter.java From TouchNews with Apache License 2.0 | 6 votes |
@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { if (holder instanceof MViewHolder) { JokeImageRoot.Contentlist contentEntity = mData.get(position); ((MViewHolder) holder).mTitle.setText(contentEntity.getTitle()); final ImageView imageView = ((MViewHolder) holder).mImage; ImageUtil.displayImage(mContext, contentEntity.getImg(), imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, ImageBrowseActivity.class); intent.putExtra("url", mData.get(position).getImg()); // View transitionView = view.findViewById(R.id.ivNews); ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((HomeActivity) mContext, imageView, mContext.getString(R.string.transition__img)); ActivityCompat.startActivity((HomeActivity) mContext, intent, options.toBundle()); } }); } }
Example #6
Source File: PhotoDetectActivity.java From FaceDetectCamera with Apache License 2.0 | 6 votes |
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBackPressed(); return true; case R.id.gallery: int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (rc == PackageManager.PERMISSION_GRANTED) { getImage(); } else { requestWriteExternalPermission(); } return true; default: return super.onOptionsItemSelected(item); } }
Example #7
Source File: ScanActivity.java From blefun-androidthings with Apache License 2.0 | 6 votes |
private void prepareForScan() { if (isBleSupported()) { // Ensures Bluetooth is enabled on the device BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter btAdapter = btManager.getAdapter(); if (btAdapter.isEnabled()) { // Prompt for runtime permission if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { startLeScan(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATION); } } else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } else { Toast.makeText(this, "BLE is not supported", Toast.LENGTH_LONG).show(); finish(); } }
Example #8
Source File: HomeActivity.java From Fatigue-Detection with MIT License | 6 votes |
private boolean checkPermission(String permission,int requestCode){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { showHint("Camera and SDCard access is required by Omoshiroi, please grant the permission in settings."); finish(); } else { ActivityCompat.requestPermissions(this, new String[]{ permission, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO }, requestCode); } return false; }else return true; } return true; }
Example #9
Source File: PermissionChecker.java From openlauncher with Apache License 2.0 | 6 votes |
public boolean doIfExtStoragePermissionGranted(String... optionalToastMessageForKnowingWhyNeeded) { if (ContextCompat.checkSelfPermission(_activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (optionalToastMessageForKnowingWhyNeeded != null && optionalToastMessageForKnowingWhyNeeded.length > 0 && optionalToastMessageForKnowingWhyNeeded[0] != null) { new AlertDialog.Builder(_activity) .setMessage(optionalToastMessageForKnowingWhyNeeded[0]) .setCancelable(false) .setNegativeButton(android.R.string.no, null) .setPositiveButton(android.R.string.yes, (dialog, which) -> { if (android.os.Build.VERSION.SDK_INT >= 23) { ActivityCompat.requestPermissions(_activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_PERMISSION_EXTERNAL_STORAGE); } }) .show(); return false; } ActivityCompat.requestPermissions(_activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_PERMISSION_EXTERNAL_STORAGE); return false; } return true; }
Example #10
Source File: PermissionHelper.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
/** * @return true if has-(runtime-)permissions. * */ public static boolean hasPermission(Activity context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { boolean needsRead = ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED; boolean needsWrite = ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED; if (needsRead || needsWrite) { // no permission yet return false; } } // if android-m // already has permission. return true; }
Example #11
Source File: SplashActivity.java From protrip with MIT License | 6 votes |
private void getLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { mCurrentLat = String.valueOf(mLastLocation.getLatitude()); mCurrentLng = String.valueOf(mLastLocation.getLongitude()); Geocoder geocoder = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(mCurrentLat), Double.parseDouble(mCurrentLng), 1); //mCurrentLocName = addresses.get(0).getLocality(); mCurrentLocName = addresses.get(0).getAddressLine(1); } catch (Exception e) { Log.d(TAG, "Exception"); } } skip(); } }
Example #12
Source File: ActivityEditLocation2.java From fingen with Apache License 2.0 | 6 votes |
@NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}) void startDetectCoords() { if (location.getID() < 0) { locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager != null && locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } if (locationManager != null && locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } }
Example #13
Source File: ProfilePictureActivity.java From kute with Apache License 2.0 | 6 votes |
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode==0 && ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED ) { if(permissions.length>1 && ActivityCompat.checkSelfPermission(this, permissions[1]) == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "We Got External Storage and Camera", Toast.LENGTH_SHORT).show(); getNewProfileImage(); } else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED) { //User just granted one of the denied permissions getNewProfileImage(); } else { Snackbar.make(cl,"You need to give permissions to setup custom profile picture",Snackbar.LENGTH_LONG).show(); } } else if(requestCode==0) { Snackbar.make(cl,"You need to give permissions to setup custom profile picture",Snackbar.LENGTH_LONG).show(); } }
Example #14
Source File: HiddenCameraActivity.java From android-hidden-camera with Apache License 2.0 | 6 votes |
/** * Start the hidden camera. Make sure that you check for the runtime permissions before you start * the camera. * * @param cameraConfig camera configuration {@link CameraConfig} */ @RequiresPermission(Manifest.permission.CAMERA) protected void startCamera(CameraConfig cameraConfig) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { //check if the camera permission is available onCameraError(CameraError.ERROR_CAMERA_PERMISSION_NOT_AVAILABLE); } else if (cameraConfig.getFacing() == CameraFacing.FRONT_FACING_CAMERA && !HiddenCameraUtils.isFrontCameraAvailable(this)) { //Check if for the front camera onCameraError(CameraError.ERROR_DOES_NOT_HAVE_FRONT_CAMERA); } else { mCachedCameraConfig = cameraConfig; mCameraPreview.startCameraInternal(cameraConfig); } }
Example #15
Source File: MainActivity.java From Android-Fused-location-provider-example with Apache License 2.0 | 6 votes |
@Override public void onConnected(@Nullable Bundle bundle) { Log.i(TAG, "onConnected"); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } if (mCurrentLocation == null) { mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); updateUI(); } if (mRequestingLocationUpdates) { startLocationUpdates(); } }
Example #16
Source File: MainActivity.java From APNG4Android with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100); findViewById(R.id.tv_1).setOnClickListener(this); findViewById(R.id.tv_2).setOnClickListener(this); findViewById(R.id.tv_3).setOnClickListener(this); findViewById(R.id.tv_4).setOnClickListener(this); findViewById(R.id.tv_5).setOnClickListener(this); findViewById(R.id.tv_6).setOnClickListener(this); findViewById(R.id.tv_7).setOnClickListener(this); findViewById(R.id.tv_8).setOnClickListener(this); }
Example #17
Source File: LocationUpdateReceiver.java From home-assistant-Android with GNU General Public License v3.0 | 6 votes |
@Override public void onConnected() { if (ActivityCompat.checkSelfPermission(tempContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (prefs.getBoolean(Common.PREF_ENABLE_LOCATION_TRACKING, false) && !TextUtils.isEmpty(prefs.getString(Common.PREF_LOCATION_DEVICE_NAME, null))) { LocationRequest locationRequest = LocationRequest.create() .setInterval(prefs.getInt(Common.PREF_LOCATION_UPDATE_INTERVAL, 10) * 60 * 1000) .setFastestInterval(5 * 60 * 1000) .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, getPendingIntent(tempContext)); Log.d(TAG, "Started requesting location updates"); } else { LocationServices.FusedLocationApi.removeLocationUpdates(apiClient, getPendingIntent(tempContext)); Log.d(TAG, "Stopped requesting location updates"); } } apiClient.disconnect(); tempContext = null; }
Example #18
Source File: IntroActivity.java From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Checks if it is allowed to use the camera. * @return boolean */ public boolean isCameraPermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { return true; } else { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION); return false; } } else { return true; } }
Example #19
Source File: RxDeviceTool.java From RxTools-master with Apache License 2.0 | 5 votes |
/** * 拨打电话 * 需添加权限 {@code <uses-permission android:name="android.permission.CALL_PHONE"/>} * * @param context 上下文 * @param phoneNumber 电话号码 */ public static void callPhone(final Context context, String phoneNumber) { if (!RxDataTool.isNullString(phoneNumber)) { final String phoneNumber1 = phoneNumber.trim();// 删除字符串首部和尾部的空格 // 调用系统的拨号服务实现电话拨打功能 // 封装一个拨打电话的intent,并且将电话号码包装成一个Uri对象传入 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber1)); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { return; } context.startActivity(intent);// 内部类 } }
Example #20
Source File: Router.java From XDroidMvp with MIT License | 5 votes |
public void launch() { try { if (intent != null && from != null && to != null) { if (callback != null) { callback.onBefore(from, to); } intent.setClass(from, to); intent.putExtras(getBundleData()); if (options == null) { if (requestCode < 0) { from.startActivity(intent); } else { from.startActivityForResult(intent, requestCode); } if (enterAnim > 0 && exitAnim > 0) { from.overridePendingTransition(enterAnim, exitAnim); } } else { if (requestCode < 0) { ActivityCompat.startActivity(from, intent, options.toBundle()); } else { ActivityCompat.startActivityForResult(from, intent, requestCode, options.toBundle()); } } if (callback != null) { callback.onNext(from, to); } } } catch (Throwable throwable) { if (callback != null) { callback.onError(from, to, throwable); } } }
Example #21
Source File: PlaybackControlsFragment.java From LyricHere with Apache License 2.0 | 5 votes |
private void onPlaybackStateChanged(PlaybackStateCompat state) { LogUtils.d(TAG, "onPlaybackStateChanged ", state); if (getActivity() == null) { LogUtils.w(TAG, "onPlaybackStateChanged called when getActivity null," + "this should not happen if the callback was properly unregistered. Ignoring."); return; } if (state == null) { return; } boolean enablePlay = false; switch (state.getState()) { case PlaybackStateCompat.STATE_PAUSED: case PlaybackStateCompat.STATE_STOPPED: enablePlay = true; break; case PlaybackStateCompat.STATE_ERROR: LogUtils.e(TAG, "error playbackstate: ", state.getErrorMessage()); Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show(); break; } if (enablePlay) { mPlayPause.setImageDrawable( ActivityCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_36dp)); } else { mPlayPause.setImageDrawable( ActivityCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_36dp)); } MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController(); String extraInfo = null; if (controller != null && controller.getExtras() != null) { String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST); if (castName != null) { extraInfo = getResources().getString(R.string.casting_to_device, castName); } } setExtraInfo(extraInfo); }
Example #22
Source File: DeviceLocation.java From ARCore-Location with MIT License | 5 votes |
public void permissionsCheck() { if (ActivityCompat.checkSelfPermission( activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { // Check Permissions Now ActivityCompat.requestPermissions( activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0); } }
Example #23
Source File: MainActivity.java From videokit-ffmpeg-android with GNU Lesser General Public License v3.0 | 5 votes |
private void setupListIfWritePermissionGranted() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE); } else { setupList(); } }
Example #24
Source File: MarshmallowPermission.java From Expert-Android-Programming with MIT License | 5 votes |
public void requestPermissionForWriteExternalStorage() { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITEEXTERNAL_STORAGE_PERMISSION_REQUEST_CODE); } else { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITEEXTERNAL_STORAGE_PERMISSION_REQUEST_CODE); } }
Example #25
Source File: XPermissionUtils.java From XPermissionUtils with Apache License 2.0 | 5 votes |
/** * 是否彻底拒绝了某项权限 */ private static boolean hasAlwaysDeniedPermission(@NonNull Context context, @NonNull String... deniedPermissions) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return false; } boolean rationale; for (String permission : deniedPermissions) { rationale = ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission); if (!rationale) { return true; } } return false; }
Example #26
Source File: CheckPermissionActivity.java From PicturePicker with Apache License 2.0 | 5 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); overridePendingTransition(0, 0); String[] needPermission = getNeedPermission(); ActivityCompat.requestPermissions(this, needPermission, REQUEST_PERMISSION); }
Example #27
Source File: OcrCaptureActivity.java From Document-Scanner with GNU General Public License v3.0 | 5 votes |
/** * Handles the requesting of the camera permission. This includes * showing a "Snackbar" message of why the permission is needed then * sending the request. */ 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); return; } final Activity thisActivity = this; View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions(thisActivity, permissions, RC_HANDLE_CAMERA_PERM); } }; Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok, listener) .show(); }
Example #28
Source File: PermissionHelper.java From open-rmbt with Apache License 2.0 | 5 votes |
protected static boolean shouldShowRequestPermissionRationale(Activity act, String[] permissions) { for (String permission : filterDynamicPermissions(permissions)) { if (ActivityCompat.shouldShowRequestPermissionRationale(act, permission)) return true; } return false; }
Example #29
Source File: MovieDetailsActivity.java From qvod with MIT License | 5 votes |
public static void startAction(Activity context, HotMovieBean.SubjectsBean positionData, ImageView imageView) { Intent intent = new Intent(context, MovieDetailsActivity.class); intent.putExtra("positionData", positionData); ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation( context, imageView, context.getResources().getString(R.string.transition_movie_img)); ActivityCompat.startActivity(context, intent, compat.toBundle()); }
Example #30
Source File: IntentUtil.java From AndroidBasicProject with MIT License | 5 votes |
/** * 直接拨号 * 需要权限:android.permission.CALL_PHONE */ public static void call(@NonNull Context context, @NonNull String phoneNumber) { Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { context.startActivity(intentPhone); } else { Logger.e("no permission: android.permission.CALL_PHONE"); } }