[JAVA] Deal with Android Runtime Permission in one line

When developing Android apps these days, we often suffer from Android Runtime Permission. Runtime by just adding one line </ font> to the correspondence of Runtime Permission I have created a library that can handle Permission. https://github.com/TakuKobayashi/RuntimePermissionChecker If you like, I'd like to receive "stars" and "likes", or "share" and spread it. M (_ _) m

Introduction method

download

build.gradle (see image below)

Added the following contents to the dependencies of

build.gradle


dependencies {
    implementation 'net.taptappun.taku.kobayashi:runtimepermissionchecker:1.1.1'
}

This will introduce the library to your Android project.

Implementation

Once the library is installed, add it to your Activity as shown in the example below.

MainActivity.java


public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        RuntimePermissionChecker.requestAllPermissions(this, REQUEST_CODE);
    }
}

this

RuntimePermissionChecker.requestAllPermissions(Activity activity, int requestCode);

After that, the process of checking Android Permission is automatically executed by adding to any place. * Since the dialog for confirming Android Permission will only appear, please do not forget to add the Permission setting to AndroidManifest.xml as shown below. </ Font> >

AndroidManifest.xml


<uses-permission android:name="Permission name" />

I want to specify Permission individually instead of all at once

MainActivity.java


public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        RuntimePermissionChecker.requestPermission(this, REQUEST_CODE, Manifest.permission.CAMERA);
    }
}

this

RuntimePermissionChecker.requestPermission(Activity activity, int requestCode, String requestPermission);

By adding to any place, the process of confirming the specified Permission is executed by specifying the Permission name you want to hear individually in the above third argument (String requestPermission). (In the above case, the camera permission confirmation screen will be displayed.)

Supplement

I want to execute the process when all Permission is permitted

In that case, [onRequestPermissionsResult](https://developer.android.com/reference/android/app/Activity.html#onRequestPermissionsResult(int, java.lang.String [], int [])) as shown in the following example. Makes the process execute when the callback of is called.

MainActivity.java


public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 1;

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (requestCode != REQUEST_CODE)
            return;
        if(!RuntimePermissionChecker.existConfirmPermissions(this)){
            // write features you want to execute.
        }
    }
}

this

RuntimePermissionChecker.existConfirmPermissions(Activity activity);

Will be ture </ font> if there are still permissions to listen to.

I want to check if individual Permission is allowed

If you want to check whether individual Permission is permitted instead of when all Permission is permitted above

RuntimePermissionChecker.hasSelfPermission(Activity activity, String requestPermission);

You can check using this method. Permission name that you want to check individually in the second argument of this method If you specify and it is already allowed, it will be ture </ font>.

What if Android 6.0 Marshmallow (API Level 23) or lower?

The above processing also includes the correspondence in this case.

What if the user has already granted permission?

The above processing also includes the correspondence in this case.

What is Runtime Permission in the first place?

Starting with Android 6.0 Marshmallow (API Level 23), you need to ask for a user's permission to use a specific feature when running the app, and you can't use the feature without that user's permission. .. (Storing information on a camera, microphone, SD card, etc.) In short, it's a dialog like this. 68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f33343438382f32303466316232632d623732362d363466612d346235662d3639343333386565613136372e706e67.png Of course, in order to display this dialog, it is necessary to write the process on the application side. See here for more details

As a person who develops many Android apps and has been developing Android apps since the time of Android 2 series, I do not want to think about implementing this Runtime Permission, so as a maximum concession, I managed to add only one line. I wanted to. Therefore, no matter what app you make

  • Put this library in for the time being
  • Call this method for the time being

So I tried to make it something that would solve everything.

  • For the first release of the Android library this time, we referred to the following various articles.

reference


Postscript 2017/12/13 (Wed) I was able to register for Bintray. I was also able to register with jcenter, so I updated the library installation method.