Permissions

Request only on debug mode

The app will write the file to internal storage, but writes to external storage for debug. That's why I have this feature. I have to declare the permission in debug mode such as below, it will bind to debug build.

  • src/debug/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.hsy.permisiondemo">

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

Then using BuildConfig.DEBUG to request the debug-only permission in Java code.

Testing permissions and an Android apps Manifest file of Stack Overflow

Request on click button

  • build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "com.hsy.permisiondemo"
        minSdkVersion 21
        targetSdkVersion 26

    ...

    dataBinding {
        enabled = true
    }
}

dependencies {
    ...

    compile 'pub.devrel:easypermissions:1.0.1'
}
  • fragment_blank.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context="com.hsy.permisiondemo.BlankFragment">

  <data>
    <variable
      name="handler"
      type="android.support.v4.app.Fragment" />

    <variable
      name="viewModel"
      type="com.hsy.permisiondemo.ViewModel" />
  </data>

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
      android:id="@+id/write_to_storage_btn"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:onClick="@{() -> viewModel.write2ExternalStorageAfterRequestPermission(handler)}"
      android:text="@string/hello_blank_fragment" />

  </FrameLayout>

</layout>
  • ViewModel
public class ViewModel {

  private static final String TAG = "ViewModel";

  static final int RC_STORAGE_PERM = 9487;

  public void write2ExternalStorageAfterRequestPermission(Fragment fragment) {
    Log.d(TAG, "write2ExternalStorageOrRequestPermission()");
    EasyPermissions.requestPermissions(
        fragment,
        "I need the write permission",
        RC_STORAGE_PERM,
        WRITE_EXTERNAL_STORAGE);
  }
}
  • Fragment
/**
 * Created in {@link R.layout#activity_main}
 */
public class BlankFragment extends Fragment {

  private static final String TAG = "BlankFragment";

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    FragmentBlankBinding binding = DataBindingUtil
        .inflate(inflater, R.layout.fragment_blank, container, false);
    binding.setHandler(this);
    binding.setViewModel(new ViewModel());
    return binding.getRoot();
  }

  @AfterPermissionGranted(ViewModel.RC_STORAGE_PERM)
  public void write2ExternalStorage() {
    Log.d(TAG, "write2ExternalStorage()");
    Toast.makeText(this.getContext(), "TODO: Storeg things", Toast.LENGTH_LONG).show();
  }
}

When I click the write_to_storage_btn, the dialog will display and ask me to allow the permission for write to external storage. After I allow it, the BlankFragment#write2ExternalStorage() will be called automatically by addingAfterPermissionGranted annotation to it. But this annotation is only work in Activity and Fragment .

  • LogCat
D/ViewModel: write2ExternalStorageAfterRequestPermission()
D/BlankFragment: write2ExternalStorage()

When I click the write_to_storage_btn second times, it will not show the dialog any more. It just call the BlankFragment#write2ExternalStorage() .

  • LogCat
D/ViewModel: write2ExternalStorageAfterRequestPermission()
D/BlankFragment: write2ExternalStorage()

easypermissions

Request on user check Never Ask Again option

After the user denied the permissions with the Never Ask Again option, I can use the method

EasyPermissions.somePermissionPermanentlyDenied()to display a dialog to the user and ask them to the system setting page for my application.

  • Fragment
public class BlankFragment extends Fragment implements EasyPermissions.PermissionCallbacks {

  ...

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
      @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // Forward results to EasyPermissions
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
  }

  @Override
  public void onPermissionsGranted(int requestCode, List<String> perms) {}

  @Override
  public void onPermissionsDenied(int requestCode, List<String> perms) {
    // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
    // This will display a dialog directing them to enable the permission in app settings.
    if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
      new AppSettingsDialog.Builder(this).build().show();
    }
  }
}

Note: EasyPermissions.onRequestPermissionsResult() and override Fragment#onRequestPermissionsResult() are also needed.

results matching ""

    No results matching ""