npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-android-permissions

v1.0.2

Published

React Native Android Permissions experimental module - Android M (6.0) permissions like to your React Native application

Downloads

29

Readme

react-native-android-permissions

React Native Android Permissions experimental module - Android M (6.0) permissions like to your React Native application.

If you need to work with Android M (6.0+) permissions model this experimental module that can be helpful to you. This works with only two methods: checkPermission and requestPermission.

FUTURE DEPRECATION WARNING: The official RN PermissionsAndroid will be realized soon (https://github.com/facebook/react-native/pull/9292) and the future documentation are here. So this module will be deprecated after that.

First of all you need to set in your android/app/build.gradle file the targetSdkVersion to 23 or a major SDK version:

...
  defaultConfig {
      applicationId "com.awesomepermissions"
      minSdkVersion 16
      targetSdkVersion 23 // <--- this change
      versionCode 1
      versionName "1.0"
      ndk {
          abiFilters "armeabi-v7a", "x86"
      }
  }

After that, when you're testing your app in development mode, you have to allow the "Draw over other apps" permission:

Setting the draw over permission

The recommended way of deal with permissions in Android is to preview and list all the permissions in your AndroidManifest.xml file:

...
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
...

Before that you can ask some of then in run time, when you need.

Demo

Installation

npm install react-native-android-permissions --save

Add it to your android project

  • In android/setting.gradle
...
include ':RNPermissionsModule', ':app'
project(':RNPermissionsModule').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-permissions/android')
  • In android/app/build.gradle
...
dependencies {
    ...
    compile project(':RNPermissionsModule')
}

React Native >= 0.29:

MainActivity.java:

import com.burnweb.rnpermissions.RNPermissionsPackage;  // <--- import

public class MainActivity extends ReactActivity {
  ......

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      RNPermissionsPackage.onRequestPermissionsResult(requestCode, permissions, grantResults); // very important event callback
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }

  ......

}

MainApplication.java:

import com.burnweb.rnpermissions.RNPermissionsPackage;  // <--- import

public class MainApplication extends Application implements ReactApplication {
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
  ......

  @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new RNPermissionsPackage() // <------ add this line to your MainApplication class
      );
    }

  ......

}

React Native <= 0.28:

MainActivity.java:

import com.burnweb.rnpermissions.RNPermissionsPackage;  // <--- import

public class MainActivity extends ReactActivity {
  ......

  @Override
  protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new RNPermissionsPackage()); // <------ add this line to your MainActivity class
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      RNPermissionsPackage.onRequestPermissionsResult(requestCode, permissions, grantResults); // very important event callback
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }

  ......

}

Example of checkPermission method:

import React, {
  AppRegistry,
  Component,
  StyleSheet,
  Text,
  View
} from 'react-native';

import {checkPermission} from 'react-native-android-permissions';

class AwesomePermissions extends Component {
  componentDidMount() {
    checkPermission("android.permission.ACCESS_FINE_LOCATION").then((result) => {
      console.log("Already Granted!");
      console.log(result);
    }, (result) => {
      console.log("Not Granted!");
      console.log(result);
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

AppRegistry.registerComponent('AwesomePermissions', () => AwesomePermissions);

Example of requestPermission method:

import React, {
  AppRegistry,
  Component,
  StyleSheet,
  Text,
  View
} from 'react-native';

import {requestPermission} from 'react-native-android-permissions';

class AwesomePermissions extends Component {
  componentDidMount() {
    setTimeout(() => {
      requestPermission("android.permission.ACCESS_FINE_LOCATION").then((result) => {
        console.log("Granted!", result);
        // now you can set the listenner to watch the user geo location
      }, (result) => {
        console.log("Not Granted!");
        console.log(result);
      });
    // for the correct StatusBar behaviour with translucent={true} we need to wait a bit and ask for permission after the first render cycle
    // (check https://github.com/facebook/react-native/issues/9413 for more info)
    }, 0);
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

AppRegistry.registerComponent('AwesomePermissions', () => AwesomePermissions);

License

MIT