private void requestPermissionForCameraAndMicrophone() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.RECORD_AUDIO)) { Toast.makeText(this, R.string.permissions_needed, Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions( this, new String[] {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, CAMERA_MIC_PERMISSION_REQUEST_CODE); } }
private void requestGplusPermission() { final String[] PERMISSIONS = {Manifest.permission.GET_ACCOUNTS, Manifest.permission.INTERNET}; if (ActivityCompat.shouldShowRequestPermissionRationale( getActivity(), Manifest.permission.GET_ACCOUNTS) || ActivityCompat.shouldShowRequestPermissionRationale( getActivity(), Manifest.permission.INTERNET)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // For example, if the request has been denied previously. String fab_skin = (BaseActivity.accentSkin); final MaterialDialog materialDialog = Futils.showBasicDialog( getActivity(), fab_skin, theme, new String[] { getResources().getString(R.string.grantgplus), getResources().getString(R.string.grantper), getResources().getString(R.string.grant), getResources().getString(R.string.cancel), null }); materialDialog .getActionButton(DialogAction.POSITIVE) .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 66); materialDialog.dismiss(); } }); materialDialog .getActionButton(DialogAction.NEGATIVE) .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { getActivity().finish(); } }); materialDialog.setCancelable(false); materialDialog.show(); } else { // Contact permissions have not been granted yet. Request them directly. ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 66); } }
private void checkForStoragePermission(int position, SimpleItemViewHolder holder) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { getWallpaper(position); } else { if (ActivityCompat.shouldShowRequestPermissionRationale( (Activity) context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Snackbar.make( holder.mainView, R.string.permission_storage_rationale, Snackbar.LENGTH_INDEFINITE) .setAction( R.string.ok, new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions( (Activity) context, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE); } }) .show(); } else { reqPos = position; ActivityCompat.requestPermissions( ((Activity) context), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE); } } }
/** * Check if we have the GET_ACCOUNTS permission and request it if we do not. * * @return true if we have the permission, false if we do not. */ private boolean checkAccountsPermission() { final String perm = Manifest.permission.GET_ACCOUNTS; int permissionCheck = ContextCompat.checkSelfPermission(getContext(), perm); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { // We have the permission return true; } else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), perm)) { // Need to show permission rationale, display a snackbar and then request // the permission again when the snackbar is dismissed. Snackbar.make( recViewLogin.findViewById(R.id.login_layout), R.string.contacts_permission_rationale, Snackbar.LENGTH_INDEFINITE) .setAction( android.R.string.ok, new View.OnClickListener() { @Override public void onClick(View v) { // Request the permission again. ActivityCompat.requestPermissions( getActivity(), new String[] {perm}, RC_PERM_GET_ACCOUNTS); } }) .show(); return false; } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(getActivity(), new String[] {perm}, RC_PERM_GET_ACCOUNTS); return false; } }
private void getPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.GET_ACCOUNTS)) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Permission Request"); builder.setMessage( "To bring you timely alerts we would require few permissions such as account details. Please grant the permissions to continue using these features"); builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); ActivityCompat.requestPermissions( MainActivity.this, new String[] {Manifest.permission.GET_ACCOUNTS}, MY_PERMISSIONS_REQUEST_GET_ACCOUNTS); } }); builder.create().show(); } else { ActivityCompat.requestPermissions( this, new String[] {Manifest.permission.GET_ACCOUNTS}, MY_PERMISSIONS_REQUEST_GET_ACCOUNTS); } } else { setupUser(); } }
void startAvatarTaker() { int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA); if (permissionCheck == PackageManager.PERMISSION_DENIED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale( getActivity(), Manifest.permission.CAMERA)) { // 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(mView, R.string.grant_perms, Snackbar.LENGTH_LONG).show(); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions( getActivity(), 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 { startActivityForResult(getPickImageChooserIntent(), 200); } }
@Override public void onPermissionsDenied( @NonNull String[] grantedPermissions, @NonNull String[] deniedPermissions, @NonNull String[] declinedPermissions) { MainSettingsActivity activity = mMainSettingsActivityWeakReference.get(); if (activity == null) return; // if the result is DENIED and the OS says "do not show rationale", it means the user has // ticked "Don't ask me again". final boolean userSaysDontAskAgain = !ActivityCompat.shouldShowRequestPermissionRationale( activity, Manifest.permission.READ_CONTACTS); // the user has denied us from reading the Contacts information. // I'll ask them to whether they want to grant anyway, or disable ContactDictionary AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setCancelable(true); builder.setIcon(R.drawable.ic_notification_contacts_permission_required); builder.setTitle(R.string.notification_read_contacts_title); builder.setMessage(activity.getString(R.string.contacts_permissions_dialog_message)); builder.setPositiveButton( activity.getString( userSaysDontAskAgain ? R.string.navigate_to_app_permissions : R.string.allow_permission), activity.mContactsDictionaryDialogListener); builder.setNegativeButton( activity.getString(R.string.turn_off_contacts_dictionary), activity.mContactsDictionaryDialogListener); if (activity.mAlertDialog != null && activity.mAlertDialog.isShowing()) activity.mAlertDialog.dismiss(); activity.mAlertDialog = builder.create(); activity.mAlertDialog.show(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "MainActivity - onCreate"); // Check that the Permission to read From External storage - if the checkselfPermission is null // than it means that the user has clicked on "Never Ask me again". if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Check if we should show the Permission RAtional behind the reason we are asking for the // Permissions if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.READ_EXTERNAL_STORAGE)) { // Show a Fragment that explains why I need this permission } else { ActivityCompat.requestPermissions( this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST_CODE); } } // Create the List from Storage getExternalRootFolderList(); // Conect the Fragment to the Activity listFragment = new FolderViewFragment(); getSupportFragmentManager() .beginTransaction() .add(R.id.fragment_container, listFragment) .commit(); }
/** * 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 not granted, requesting..."); final String[] permissions = new String[] {Manifest.permission.CAMERA}; if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { Log.d(TAG, "We do not need to show permission rationale"); ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); return; } final Activity thisActivity = this; Log.i(TAG, "Showing Request Permissions Rationale"); 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(android.R.string.ok, listener) .show(); }
@SuppressWarnings("SameParameterValue") private void perm(String permission, int permission_request) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { ActivityCompat.requestPermissions(this, new String[] {permission}, permission_request); } } }
public static boolean shouldShowRationale(Activity activity, String... permissions) { for (String permission : permissions) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { return true; } } return false; }
public void requestInternetPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale( activity, Manifest.permission.INTERNET)) { permissionCallback.onPermissionExplanationRequired(true, Manifest.permission.INTERNET); } else { ActivityCompat.requestPermissions( activity, new String[] {Manifest.permission.INTERNET}, INTERNET_PERMISSION); } }
/** * Requests the fine location permission. If a rationale with an additional explanation should be * shown to the user, displays a dialog that triggers the request. */ public static void requestPermission( AppCompatActivity activity, int requestId, String permission, boolean finishActivity) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { // Display a dialog with rationale. RationaleDialog.newInstance(requestId, finishActivity) .show(activity.getSupportFragmentManager(), "dialog"); } else { // Location permission has not been granted yet, request it. ActivityCompat.requestPermissions(activity, new String[] {permission}, requestId); } }
public void requestExternalStoragePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale( activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { permissionCallback.onPermissionExplanationRequired( true, Manifest.permission.WRITE_EXTERNAL_STORAGE); } else { ActivityCompat.requestPermissions( activity, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION); } }
private void requestPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Snackbar.make( mainContent, "Permission is required to continue using this " + "function!", Snackbar.LENGTH_INDEFINITE) .setAction( "Okay", new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions(Home.this, EXTERNAL_STORAGE, REQUEST_STORAGE); } }) .show(); } else { ActivityCompat.requestPermissions(this, EXTERNAL_STORAGE, REQUEST_STORAGE); } }
public static boolean checkPermission(Activity activity, String permission, int code) { if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { ActivityCompat.requestPermissions(activity, new String[] {permission}, code); } else { ActivityCompat.requestPermissions(activity, new String[] {permission}, code); } return false; } return true; }
/** Convenience method to ask user to grant permissions. */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) protected void requestPermissions() { boolean shouldShowRationale = ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.RECORD_AUDIO); if (shouldShowRationale) { CommonUtils.getInstance().showMessage(R.string.Grant_necessary_permissions); } else { ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO }, MY_PERMISSIONS_REQUEST); } }
public void requestExternalStoragePermission( AppCompatActivity appCompatActivity, PermissionCallback callback, boolean requestFromDialog) { if (ActivityCompat.shouldShowRequestPermissionRationale( appCompatActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !requestFromDialog) { callback.onPermissionExplanationRequired(true, Manifest.permission.WRITE_EXTERNAL_STORAGE); } else { ActivityCompat.requestPermissions( appCompatActivity, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_GRANTED) { openFragment(); } else { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.RECORD_AUDIO) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.MODIFY_AUDIO_SETTINGS)) { AlertDialog.OnClickListener onClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { requestPermissions(); } else if (which == DialogInterface.BUTTON_NEGATIVE) { permissionsNotGranted(); } } }; new AlertDialog.Builder(this) .setTitle(getString(R.string.title_permissions)) .setMessage(Html.fromHtml(getString(R.string.message_permissions))) .setPositiveButton(getString(R.string.btn_next), onClickListener) .setNegativeButton(getString(R.string.btn_cancel), onClickListener) .show(); } else { requestPermissions(); } } } }
private void requestLocationPermission() { if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (!ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.ACCESS_COARSE_LOCATION)) { // User Denied and selected "Don't ask again" --> Show info that he needs to go to settings showSettingsDialog(); } else { ActivityCompat.requestPermissions( this, MainActivity.PERMISSIONS_LOCATION, MainActivity.REQUEST_LOCATION); } } else { Snackbar.make(mLayout, "You already have this permission, FOOL!", Snackbar.LENGTH_SHORT); } }
private void requestNetworkStatePermission() { // Permission has not been granted and must be requested. /** * if (ActivityCompat.shouldShowRequestPermissionRationale(this, * Manifest.permission.ACCESS_NETWORK_STATE)) { // Provide an additional rationale to the user * if the permission was not granted // and the user would benefit from additional context for * the use of the permission. // Display a SnackBar with a button to request the missing * permission. Snackbar.make(mLayout, "Permission Access Network State is Required.", * Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() { @Override public * void onClick(View view) { // Request the permission * ActivityCompat.requestPermissions(MainActivity.this, new * String[]{Manifest.permission.ACCESS_NETWORK_STATE}, PERMISSION_REQUEST_NETWORK_STATE); } * }).show(); * * <p>} else { Snackbar.make(mLayout, "Permission Access Network State is not available. * Requesting permission.", Snackbar.LENGTH_SHORT).show(); // Request the permission. The result * will be received in onRequestPermissionResult(). ActivityCompat.requestPermissions(this, new * String[]{Manifest.permission.ACCESS_NETWORK_STATE}, PERMISSION_REQUEST_NETWORK_STATE); } */ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // Display a SnackBar with a button to request the missing permission. Snackbar.make(mLayout, "Permission Internet is Required.", Snackbar.LENGTH_INDEFINITE) .setAction( "OK", new View.OnClickListener() { @Override public void onClick(View view) { // Request the permission ActivityCompat.requestPermissions( MainActivity.this, new String[] {Manifest.permission.INTERNET}, PERMISSION_REQUEST_INTERNET); } }) .show(); } else { Snackbar.make( mLayout, "Internet is not available. Requesting permission.", Snackbar.LENGTH_SHORT) .show(); // Request the permission. The result will be received in onRequestPermissionResult(). ActivityCompat.requestPermissions( this, new String[] {Manifest.permission.INTERNET}, PERMISSION_REQUEST_INTERNET); } }
/** Fires the permission rationale dialog or the permission request dialog. */ private void firePermissionRequest() { String locationPermission = Manifest.permission.ACCESS_FINE_LOCATION; if (mRationaleDialog == null) { boolean rationale = ActivityCompat.shouldShowRequestPermissionRationale(this, locationPermission); if (rationale) { // Show the rationale dialog mRationaleDialog = new AlertDialog.Builder(this) .setTitle(R.string.later_location_rationale_title) .setMessage(R.string.later_location_rationale) .setPositiveButton(R.string.later_location_rationale_button, this) .setOnCancelListener(this) .create(); mRationaleDialog.show(); } else { if (SharedPreferencesManager.isFirstLocationPermissionRequest(this)) { // Request permissions SharedPreferencesManager.locationPermissionRequested(this); ActivityCompat.requestPermissions( this, new String[] {locationPermission}, LOCATION_PERMISSION_RC); mRationaleDialog = null; } else { // If this is not the first time but rationale is false, it means that the // user has tapped never ask again. In this case, if the user really // wants this feature he will have to go to settings and enable location // manually, otherwise, this will not work at all. mLocationDisabledDialog = new AlertDialog.Builder(this) .setTitle(R.string.later_location_disabled_title) .setMessage(R.string.later_location_disabled) .setPositiveButton(R.string.later_location_disabled_button_positive, this) .setNegativeButton(R.string.later_location_disabled_button_negative, this) .setOnCancelListener(this) .create(); mLocationDisabledDialog.show(); } } } else { // Request permissions SharedPreferencesManager.locationPermissionRequested(this); ActivityCompat.requestPermissions( this, new String[] {locationPermission}, LOCATION_PERMISSION_RC); mRationaleDialog = null; } }
private boolean checkForPermissions() { int canI = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); if (canI != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.READ_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.READ_EXTERNAL_STORAGE}, 33); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } return false; } return true; }
@Override public void onClick(DialogInterface dialog, final int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: if (ActivityCompat.shouldShowRequestPermissionRationale( MainSettingsActivity.this, Manifest.permission.READ_CONTACTS)) { startContactsPermissionRequest(); } else { startAppPermissionsActivity(); } break; case DialogInterface.BUTTON_NEGATIVE: SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); final SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(getString(R.string.settings_key_use_contacts_dictionary), false); SharedPreferencesCompat.EditorCompat.getInstance().apply(editor); break; } }
private void requestPermission() { // Permission has not been granted and must be requested. if (ActivityCompat.shouldShowRequestPermissionRationale( mActivity, Manifest.permission.ACCESS_FINE_LOCATION)) { ActivityCompat.requestPermissions( mActivity, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST); } else { if (mOnPermissionListener != null) { mOnPermissionListener.OnPermissionChanged(false); } // Request the permission. The result will be received in onRequestPermissionResult(). ActivityCompat.requestPermissions( mActivity, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST); } }
/** * Request a set of permissions, showing rationale if the system requests it. * * @param activity Activity requesting permissions. Should implement {@link * android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback} * @param rationale a message explaining why the application needs this set of permissions, will * be displayed if the user rejects the request the first time. * @param requestCode request code to track this request, must be < 256. * @param perms a set of permissions to be requested. */ public static void requestPermissions( final Activity activity, String rationale, final int requestCode, final String... perms) { // Check if all permissions were already granted. if (hasPermissions(activity.getApplicationContext(), perms)) { return; } boolean shouldShowRationale = false; for (String perm : perms) { shouldShowRationale = shouldShowRationale || ActivityCompat.shouldShowRequestPermissionRationale(activity, perm); } if (shouldShowRationale) { AlertDialog dialog = new AlertDialog.Builder(activity) .setMessage(rationale) .setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { executePermissionsRequest(activity, perms, requestCode); } }) .setNegativeButton( android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do nothing, user does not want to request } }) .create(); dialog.show(); } else { executePermissionsRequest(activity, perms, requestCode); } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static boolean checkPermission(final Context context) { int currentAPIVersion = Build.VERSION.SDK_INT; if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( (Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); alertBuilder.setCancelable(true); alertBuilder.setTitle("Permission necessary"); alertBuilder.setMessage("External storage permission is necessary"); alertBuilder.setPositiveButton( android.R.string.yes, new DialogInterface.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions( (Activity) context, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } }); AlertDialog alert = alertBuilder.create(); alert.show(); } else { ActivityCompat.requestPermissions( (Activity) context, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } return false; } else { return true; } } else { return true; } }
@Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode != PERMISSIONS_REQUEST_CODE) { return; } for (int i = 0; i < permissions.length; i++) { String permission = permissions[i]; if (grantResults[i] == PackageManager.PERMISSION_DENIED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { Log.i(TAG, "Permission denied without 'NEVER ASK AGAIN': " + permission); showRequestPermissionsSnackbar(); } else { Log.i(TAG, "Permission denied with 'NEVER ASK AGAIN': " + permission); showLinkToSettingsSnackbar(); } } else { Log.i(TAG, "Permission granted, do something"); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_tabs_pager); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); mTabsAdapter.addTab(mTabHost.newTabSpec("Corona").setIndicator("Corona"), CoronaFragment.class); // Based on: http://developer.android.com/training/permissions/requesting.html#perm-request // Check for the READ_CONTACTS permission before trying // to make a CursorLoader with the contact list. int readContactsPermissionState = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS); if (readContactsPermissionState == PackageManager.PERMISSION_DENIED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.READ_CONTACTS)) { // Show an explanation 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. final FragmentActivity thisActivity = this; AlertDialog.Builder builder = new AlertDialog.Builder(this); DialogInterface.OnClickListener requestClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int which) { // Request the READ_CONTACTS permission! ActivityCompat.requestPermissions( thisActivity, new String[] {Manifest.permission.READ_CONTACTS}, READ_CONTACTS_REQUEST_CODE); } }; // Compose the message for this alert. builder.setTitle("Corona Cards Tabs Sample Needs Permission"); builder.setMessage( "To make the native tab for this sample, access to the " + "device's Contacts is needed! Request access now?"); builder.setPositiveButton("Request", requestClickListener); builder.setNegativeButton(android.R.string.cancel, null); AlertDialog readContactsRationaleDialog = builder.create(); readContactsRationaleDialog.setCanceledOnTouchOutside(false); readContactsRationaleDialog.show(); } else { // Request the READ_CONTACTS permission! ActivityCompat.requestPermissions( this, new String[] {Manifest.permission.READ_CONTACTS}, READ_CONTACTS_REQUEST_CODE); } } else { // We're safe to create the native tab. createNativeTab(); } if (savedInstanceState != null) { mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); } }
public void checkIn(View view) { boolean gps_enabled = false; boolean network_enabled = false; int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { } try { network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { } if (!gps_enabled || !network_enabled) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setMessage("GPS and/or network connection not enabled, please enable to continue"); dialog.setPositiveButton( "Location settings", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int parameter) { Intent inte = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(inte); } }); dialog.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int parameter) {} }); dialog.show(); } else { if (currentapiVersion >= android.os.Build.VERSION_CODES.M) { if (permissionCheck == PackageManager.PERMISSION_DENIED) { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.ACCESS_FINE_LOCATION)) { // 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( this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } } else { initGPS(); finishTracking(); } } else { initGPS(); finishTracking(); } } }