3

I am trying to enable and disable the offline mode automatically in android using react-native code, I want to know whether is there any possibility like that in react-native, If possible can any one give me suggestions that how can i achieve it. Any help must be much appreciated.

Thank you.

Hussian Shaik
  • 2,559
  • 9
  • 37
  • 57
  • Are you want to handling Offline and online status in React Native ? – Prabhu May 22 '18 at 05:32
  • @Deva yes i want to handle offline and online status of aeroplane mode(flight mode) – Hussian Shaik May 22 '18 at 05:39
  • Possible duplicate of [How to programmatically enable and disable Flight mode on Android 4.2?](https://stackoverflow.com/questions/13766909/how-to-programmatically-enable-and-disable-flight-mode-on-android-4-2) – Ravi Raj May 22 '18 at 06:28

3 Answers3

1

Try this link https://www.npmjs.com/package/react-native-system-setting

  import SystemSetting from 'react-native-system-setting'

       SystemSetting.isAirplaneEnabled().then((enable)=>{
        const state = enable ? 'On' : 'Off';
        console.log('Current airplane is ' + state);
    })

    SystemSetting.switchAirplane(()=>{
        console.log('switch airplane successfully');
    })
Prabhu
  • 1,057
  • 7
  • 13
0

cannot switch since 4.2 version

If your app is currently making changes to settings previously defined in Settings.System (such as AIRPLANE_MODE_ON), then you should expect that doing so will no longer work on a device running Android 4.2 or higher

https://developer.android.com/about/versions/android-4.2

Andrea Ciccotta
  • 598
  • 6
  • 16
-1

You can use the react native Netinfo to find the device ofline and online. Refer the below link https://medium.com/dailyjs/offline-notice-in-react-native-28a8d01e8cd0

import React, { PureComponent } from 'react';
import { View, Text, NetInfo, Dimensions, StyleSheet } from 'react-native';

const { width } = Dimensions.get('window');

function MiniOfflineSign() {
  return (
    <View style={styles.offlineContainer}>
      <Text style={styles.offlineText}>No Internet Connection</Text>
    </View>
  );
}

class OfflineNotice extends PureComponent {
  state = {
    isConnected: true
  };

  componentDidMount() {
    NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
  }

  componentWillUnmount() {
    NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);
  }

  handleConnectivityChange = isConnected => {
    if (isConnected) {
      this.setState({ isConnected });
    } else {
      this.setState({ isConnected });
    }
  };

  render() {
    if (!this.state.isConnected) {
      return <MiniOfflineSign />;
    }
    return null;
  }
}

const styles = StyleSheet.create({
  offlineContainer: {
    backgroundColor: '#b52424',
    height: 30,
    justifyContent: 'center',
    alignItems: 'center',
    flexDirection: 'row',
    width,
    position: 'absolute',
    top: 30
  },
  offlineText: { color: '#fff' }
});

export default OfflineNotice;
Prabhu
  • 1,057
  • 7
  • 13