Skip to content

Commit 866fcef

Browse files
authored
Merge pull request #24 from rmrs/fix_example_event_listeners
Fix events listeners on android
2 parents 1f1f83a + f3f4ae7 commit 866fcef

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

README.md

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# react-native-settings
33

4-
## Still very much in Alpha!
4+
## Still very much in Alpha
55

66
We created this module to allow us to query for specific device settings.
77
For example we wanted to know if the GPS is on/off without using 'react-native'
@@ -16,8 +16,8 @@ This way we can prompt the user to go to the correct place in the settings
1616
application and make sure our application is aware that the user disables/enables
1717
a setting or denies/grants a permission.
1818

19-
Currently we've only added a way to extract the 'location' setting.
20-
We will add more in the future based on our requirements.
19+
Currently we've only added a way to extract the 'location' setting (and airplane mode on Android).
20+
We will add more in the future based on requirements.
2121

2222
[`react-native example`](https://github.com/rmrs/react-native-settings/tree/master/example) for both Android and iOS.
2323

@@ -30,29 +30,35 @@ We will add more in the future based on our requirements.
3030
`$ react-native link react-native-settings`
3131

3232
#### Android
33-
In your manifest file under:
3433

35-
``` xml
36-
<application>
37-
```
38-
add the following:
39-
40-
``` xml
41-
<receiver android:name="io.rumors.reactnativesettings.receivers.GpsLocationReceiver">
42-
<intent-filter>
43-
<action android:name="android.location.PROVIDERS_CHANGED" />
44-
<category android:name="android.intent.category.DEFAULT" />
45-
</intent-filter>
46-
</receiver>
47-
48-
<receiver android:enabled="true" android:name="io.rumors.reactnativesettings.receivers.AirplaneModeReceiver">
49-
<intent-filter>
50-
<action android:name="android.intent.action.AIRPLANE_MODE"/>
51-
</intent-filter>
52-
</receiver>
34+
In your `MainApplication.java` file register the receivers:
35+
36+
``` java
37+
...
38+
39+
import android.content.IntentFilter;
40+
import io.rumors.reactnativesettings.RNSettingsPackage;
41+
import io.rumors.reactnativesettings.receivers.GpsLocationReceiver;
42+
import io.rumors.reactnativesettings.receivers.AirplaneModeReceiver;
43+
44+
...
45+
46+
public class MainApplication extends Application implements ReactApplication {
47+
48+
...
49+
50+
@Override
51+
public void onCreate() {
52+
53+
...
54+
55+
registerReceiver(new GpsLocationReceiver(), new IntentFilter("android.location.PROVIDERS_CHANGED"));
56+
registerReceiver(new AirplaneModeReceiver(), new IntentFilter("android.intent.action.AIRPLANE_MODE"));
57+
}
58+
}
5359
```
54-
### Manual installation
5560

61+
### Manual installation
5662

5763
#### iOS
5864

@@ -64,22 +70,29 @@ add the following:
6470
#### Android
6571

6672
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
67-
- Add `import io.rumors.reactnativesettings.RNSettingsPackage;` to the imports at the top of the file
68-
- Add `new RNSettingsPackage()` to the list returned by the `getPackages()` method
73+
74+
- Add `import io.rumors.reactnativesettings.RNSettingsPackage;` to the imports at the top of the file
75+
- Add `new RNSettingsPackage()` to the list returned by the `getPackages()` method
76+
6977
2. Append the following lines to `android/settings.gradle`:
70-
```
71-
include ':react-native-settings'
72-
project(':react-native-settings').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-settings/android')
73-
```
78+
79+
```java
80+
include ':react-native-settings'
81+
project(':react-native-settings').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-settings/android')
82+
```
83+
7484
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
75-
```
76-
compile project(':react-native-settings')
77-
```
85+
86+
```java
87+
implementation project(':react-native-settings')
88+
```
7889

7990
## Usage
80-
#### Android and iOS
8191

82-
##### Getting a setting:
92+
### Android and iOS
93+
94+
#### Getting a setting
95+
8396
```javascript
8497
import RNSettings from 'react-native-settings'
8598

@@ -92,7 +105,7 @@ RNSettings.getSetting(RNSettings.LOCATION_SETTING).then(result => {
92105
})
93106
```
94107

95-
#### Android only:
108+
#### Android only
96109

97110
```javascript
98111
import RNSettings from 'react-native-settings'
@@ -107,6 +120,7 @@ RNSettings.getSetting(RNSettings.AIRPLANE_MODE_SETTING).then(result => {
107120
```
108121

109122
##### Open settings application in a specific setting
123+
110124
```javascript
111125
import RNSettings from 'react-native-settings'
112126

@@ -124,6 +138,7 @@ if (result === RNSettings.ENABLED) {
124138
```
125139
126140
##### Listen to setting change event (when applicable)
141+
127142
```javascript
128143
import RNSettings from 'react-native-settings'
129144
import { DeviceEventEmitter } from 'react-native'

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@
1313
android:allowBackup="true"
1414
android:label="@string/app_name"
1515
android:icon="@mipmap/ic_launcher"
16-
android:theme="@style/AppTheme">
17-
18-
<receiver android:name="io.rumors.reactnativesettings.receivers.GpsLocationReceiver">
19-
<intent-filter>
20-
<action android:name="android.location.PROVIDERS_CHANGED" />
21-
<category android:name="android.intent.category.DEFAULT" />
22-
</intent-filter>
23-
</receiver>
24-
25-
<receiver android:enabled="true" android:name="io.rumors.reactnativesettings.receivers.AirplaneModeReceiver">
26-
<intent-filter>
27-
<action android:name="android.intent.action.AIRPLANE_MODE"/>
28-
</intent-filter>
29-
</receiver>
16+
android:theme="@style/AppTheme">
3017

3118
<activity
3219
android:name=".MainActivity"

example/android/app/src/main/java/com/example/MainApplication.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.example;
22

33
import android.app.Application;
4+
import android.content.IntentFilter;
45

5-
import com.facebook.react.ReactApplication;
66
import io.rumors.reactnativesettings.RNSettingsPackage;
7+
import io.rumors.reactnativesettings.receivers.GpsLocationReceiver;
8+
import io.rumors.reactnativesettings.receivers.AirplaneModeReceiver;
9+
10+
import com.facebook.react.ReactApplication;
711
import com.facebook.react.ReactNativeHost;
812
import com.facebook.react.ReactPackage;
913
import com.facebook.react.shell.MainReactPackage;
@@ -43,5 +47,8 @@ public ReactNativeHost getReactNativeHost() {
4347
public void onCreate() {
4448
super.onCreate();
4549
SoLoader.init(this, /* native exopackage */ false);
50+
51+
registerReceiver(new GpsLocationReceiver(), new IntentFilter("android.location.PROVIDERS_CHANGED"));
52+
registerReceiver(new AirplaneModeReceiver(), new IntentFilter("android.intent.action.AIRPLANE_MODE"));
4653
}
4754
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-settings",
3-
"version": "v0.0.1-alpha6",
3+
"version": "v0.0.1-alpha7",
44
"description": "Access iOS and Android device settings from React Native",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)