我正在使用React Native(^0.35.0)制作Android应用程序.在某些情况下,我需要判断是否存在互联网连接.如果没有互联网连接,那么我需要显示一个按钮,将打开设备设置(通过该按钮,用户可以启用他的连接).

我正在使用react native提供的Linking个库.

我正在try 以下方式:

componentWillMount(){
    Linking.canOpenURL('app-settings:')
      .then(supported => {
        if (!supported) {
          console.log('Can\'t handle url: ' + url);
       } else {
       return Linking.openURL('app-settings:');
      }
  }).catch(err => console.error('An error occurred', err));
}

然后上面的代码给出了-console Can't handle url:app-settings:

当我试着如下:

componentWillMount(){
    Linking.canOpenURL('app-settings:')
      .then(supported => {
        return Linking.openURL('app-settings:');
      }).catch(err => console.error('An error occurred', err));
    }

以上代码给出-Error: Could not open URL 'app-settings:': No Activity found to handle Intent { act=android.intent.action.VIEW dat=app-settings: flg=0x10000000 }

这里有我遗漏的东西吗?或者是否需要更改任何其他文件,如AndroidMainfest.xmlMainActivity.java等..

推荐答案

我使用了相同的步骤打开设备网络运营商设置.以上步骤非常有用.

Here is my code.

package com.<projectname>.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);
  }
}

你只需要在上面的代码中输入常量(比如ACTION_NETWORK_OPERATOR_SETTINGS),然后创建你自己的函数,比如openNetworkSettings.其他步骤4、5和6相同.

Here are the list of constants:

React-native相关问答推荐

SignalR 与 React Native Expo:: 什么都没有发生

如何为我的 React Native Android 应用程序使用 Crashlytics?

子视图覆盖的容器borderRadius,这是一个bug吗?

如何在 Crashlytics (Fabrics) 中有效地对非致命异常进行分组?

如何在 react-native 中在图像周围添加阴影

用于 React-Native 开发的 Visual Studio Code 中的自动导入

React-Native 构建失败的应用程序:mergeDebugAssets

React Native 中的全屏图像

java.lang.ClassNotFoundException:在路径上找不到类.MainActivity:DexPathList react-native

我们如何将 Firebase Analytics 与基于 expo 的 react-native 应用程序一起使用

React native Redux - 对象不是构造函数(判断'new ctor(props context)')

当组件在react-native 中重新呈现时,动态不透明度不会改变

react-native 自动完成文本输入

React Native + React (for web) 种子元素

我可以从react-native元素中删除 tvOS 吗?

React Context:TypeError:render is not a function

React Native - 为什么我需要 babel 或 webpack?

无法解析模块react-native-screen

发布未使用 JavaScript 代码更新的 APK

React Native 的最佳文件 Select 器