问题描述
我正在使用 React-Native (^0.35.0) 来制作 Android 应用程序.在某些情况下,我需要检查互联网连接是否存在.如果没有互联网连接,那么我需要显示一个按钮来打开设备设置(用户可以通过该按钮启用他的连接).
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 internet connectivity then I need show a button which will open device settings (through which user can enable his connectivity).
我正在使用 react-native 提供的 Linking 库.
I'm Using Linking library provided by react-native.
我正在尝试以下方式:
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));
}
然后上面的代码给出-控制台无法处理 url:app-settings:
then above code gives- 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));
}
以上代码给出 - 错误:无法打开 URL 'app-settings:': 没有找到处理 Intent { act=android.intent.action.VIEW dat=app-settings: flg=0x10000000 }
这里有什么我遗漏的吗?或者是否需要更改任何其他文件,例如 AndroidMainfest.xml、MainActivity.java 等.
Is there anything that I am missing here? or Is there is need to change in any other file like AndroidMainfest.xml, MainActivity.java, etc..
推荐答案
我使用了相同的步骤来打开设备网络运营商设置.以上步骤很有帮助.
I have used the same steps for opening device network operator settings. The steps above are quite helpful.
这是我的代码.
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 相同.
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.
以下是常量列表:https://developer.android.com/reference/android/provider/Settings.html
这篇关于React-Native:无法从我的 android 应用程序打开设备设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!