EasyPermissions


Source link: https://github.com/googlesamples/easypermissions

EasyPermissions

EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher.

Installation

EasyPermissions is installed by adding the following dependency to your build.gradle file:

dependencies {

  compile 'pub.devrel:easypermissions:1.0.1' 
}

Usage

Basic

To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResult method:

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

  
}

@Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

 // Forward results to EasyPermissions

EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);

  
}
 
}

Request Permissions

The example below shows how to request permissions for a method that requires both CAMERA and ACCESS_FINE_LOCATION permissions. There are a few things to note:

  • Using EasyPermissions#hasPermissions(...) to check if the app already has the required permissions. This method can take any number of permissions as its final argument.
  • Requesting permissions with EasyPermissions#requestPermissions. This method will request the system permissions and show the rationale string provided if necessary. The request code provided should be unique to this request, and the method can take any number of permissions as its final argument.
  • Use of the AfterPermissionGranted annotation. This is optional, but provided for convenience. If all of the permissions in a given request are granted, any methods annotated with the proper request code will be executed. This is to simplify the common flow of needing to run the requesting method after all of its permissions have been granted. This can also be achieved by adding logic on the onPermissionsGranted callback.
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION) private void methodRequiresTwoPermission() {

  String[] perms = {
Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION
}
;
  if (EasyPermissions.hasPermissions(this, perms)) {

// Already have permission, do the thing

// ...
  
}
 else {

// Do not have permissions, request them now

EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),

  RC_CAMERA_AND_LOCATION, perms);

  
}
 
}

Optionally, for a finer control, you can have your Activity / Fragment implement the PermissionCallbacks interface.

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

  
}

@Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, 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> list) {

// Some permissions have been granted

// ...
  
}

@Override
  public void onPermissionsDenied(int requestCode, List<String> list) {

// Some permissions have been denied

// ...
  
}
 
}

Required Permissions

In some cases your app will not function properly without certain permissions. If the user denies these permissions with the "Never Ask Again" option, you will be unable to request these permissions from the user and they must be changed in app settings. You can use the method EasyPermissions.somePermissionPermanentlyDenied(...) to display a dialog to the user in this situation and direct them to the system setting screen for your app:

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

  Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

// (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();

  
}
 
}
  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);

if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {

// Do something after user returned from app settings screen, like showing a Toast.

Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)

  .show();

  
}
 
}

LICENSE

 Copyright 2017 Google
  Licensed under the Apache License, Version 2.0 (the "License");

 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.  

Resources

This is a wrapper that makes getting weather information from OpenWeatherMap a whole lot easier. What seperates this from other wrappers is that in addition to the basic weather information, it allows free openweathermap user to get up to 5 days of daily forecast information, which typically requires a developer account with OpenWeatherMap(180USD/month). Please note that this is not a hack, instead the library calculates the 3-hour forecast information that free users get, summarize those values, and return it as daily forecast.

Weather Android App using apixu API.

Matisse is a well-designed local image and video selector for Android. You can

  • Use it in Activity or Fragment
  • Select images including JPEG, PNG, GIF and videos including MPEG, MP4
  • Apply different themes, including two built-in themes and custom themes
  • Different image loaders
  • Define custom filter rules

Meepo is a retrofit-like router generator for Android. You can use it to create routers for Activities, Fragments and even any things.

Use multiple analytics and other trackers in your Application easily.

Swipe3DRotateView is a class designed to simplify the implementation of 3D Flip Rotation in android based on swipe gestures.

Swipe3DRotateView is extended from a Framelayout and should exactly contain 2 views as its children. It detects swipe gestures on its child view, and based on the swipe direction, rotates the children in a 3D manner on their X or Y axis.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes