React-Native: Unable to open device settings from my android app

后端 未结 2 568
情歌与酒
情歌与酒 2020-12-14 21:44

I\'m using React-Native (^0.35.0) for making Android application. There is scenario where I need to check whether internet connectivity is present or not. If there is no int

2条回答
  •  再見小時候
    2020-12-14 22:01

    I have used the same steps for opening device network operator settings. The steps above are quite helpful.

    Here is my code.

    package com..opensettings;
    
    import android.app.Activity;
    import android.content.Intent;
    
    import com.facebook.react.bridge.Callback;
    import com.facebook.react.bridge.ReactApplicationContext;
    import com.facebook.react.bridge.ReactMethod;
    import com.facebook.react.bridge.ReactContextBaseJavaModule;
    
    public class OpenSettingsModule extends ReactContextBaseJavaModule {
    
      @Override
      public String getName() {
        /**
         * return the string name of the NativeModule which represents this class in JavaScript
         * In JS access this module through React.NativeModules.OpenSettings
         */
        return "OpenSettings";
      }
    
      @ReactMethod
      public void openNetworkSettings(Callback cb) {
        Activity currentActivity = getCurrentActivity();
    
        if (currentActivity == null) {
          cb.invoke(false);
          return;
        }
    
        try {
          currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
          cb.invoke(true);
        } catch (Exception e) {
          cb.invoke(e.getMessage());
        }
      }
    
    
      /*This is for open location Settings*/
      @ReactMethod
      public void openLocationSettings(Callback cb) {
        Activity currentActivity = getCurrentActivity();
    
        if (currentActivity == null) {
          cb.invoke(false);
          return;
        }
    
        try {
          currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
          cb.invoke(true);
        } catch (Exception e) {
          cb.invoke(e.getMessage());
        }
      }
    
      /* constructor */
      public OpenSettingsModule(ReactApplicationContext reactContext) {
        super(reactContext);
      }
    }
    

    All you need to do is just put the constant(like ACTION_NETWORK_OPERATOR_SETTINGS) in above code and create your own function like one which is created above i.e. openNetworkSettings. Other steps 4, 5 and 6 are same.

    Here are the list of constants: https://developer.android.com/reference/android/provider/Settings.html

提交回复
热议问题