如何在React Native Android中打开应用程序设置页面?

27

最终,React Native Bridge是唯一的方法。但是是否有任何npm包已经成功地为任何人工作过?

8个回答

44

1
它对我不起作用(类型“LinkingStatic”上不存在属性“openSettings”)。 - Pragati
你正在使用TypeScript吗? - ridvanaltun
1
如果你正在使用expo,它将无法工作,请参考expo文档:https://docs.expo.io/versions/latest/workflow/linking/。 - ridvanaltun
openSettings() 适用于 react-native 59.0 及以上版本。也许您的 react-native 版本不足以使用该方法。顺便说一下,这个错误来自 TypeScript。 - ridvanaltun
我正在使用React Native版本0.60。但是无论如何,openSettings()对我来说都很好用。 - Pragati

18

5
可以在不使用额外库的情况下打开设置,如下所示 - Linking.openSettings() - Freewalker
嘿,这个很好用,但我想让用户手动进入应用程序设置来授予权限。但是当使用你提供的方法打开应用程序设置后,手动授予权限并返回应用程序时,它不会重新渲染或更新权限。我需要去另一个屏幕,然后回到这个屏幕才能更新应用程序的权限。 请帮忙。 - Shail Patel

14

结合现有答案:这对我在iOS和Android上都有效(不使用expo)。

import { Linking, Platform } from 'react-native';


const handleOpenSettings = () => {
    if (Platform.OS === 'ios') {
      Linking.openURL('app-settings:');
    } else {
      Linking.openSettings();
    }
};

// add a button that calls handleOpenSetttings()

4
在我的Expo(v47.0.3)上运行正常。 - Bilaal Rashid

4

来自react-native核心的链接API提供了向Android发送自定义意图操作的功能。

注意-您可以发送任何可被Android接受的自定义操作,而且还可以将数据传递给意图。

以下是打开设置的示例:

import {Linking} from 'react-native';

// To open settings
Linking.sendIntent('android.settings.SETTINGS');

// To open GPS Location setting
Linking.sendIntent('android.settings.LOCATION_SOURCE_SETTINGS');

希望这能帮到你或其他人。谢谢!
愉快的编码 :-)

3

使用

IntentLauncher.startActivity({
  action: 'android.settings.SETTINGS',
})

替代

IntentLauncher.startActivity({
  action: 'android.settings.APPLICATION_DETAILS_SETTINGS',
  data: 'package:' + pkg
})

喜欢

import IntentLauncher, { IntentConstant } from 'react-native-intent-launcher'
const openAppSettings = () => {
  if (Platform.OS === 'ios') {
    Linking.openURL('app-settings:')
  } else {
    IntentLauncher.startActivity({
      action: 'android.settings.SETTINGS',
    })
  }
}

安卓

无线网络

IntentLauncher.startActivity({
 action: 'android.settings.SETTINGS'
})

GPS

IntentLauncher.startActivity({
 action: 'android.settings.LOCATION_SOURCE_SETTINGS'
})

iOS

无线网络

Linking.openURL("App-Prefs:root=WIFI");

GPS

Linking.openURL('App-Prefs:Privacy&path=LOCATION')

1

这是一个适用于Android和iOS的完整答案。

没有使用Expo:

import DeviceInfo from 'react-native-device-info';
import IntentLauncher, { IntentConstant } from 'react-native-intent-launcher'
const pkg = DeviceInfo.getBundleId();
const openAppSettings = () => {
  if (Platform.OS === 'ios') {
    Linking.openURL('app-settings:')
  } else {
    IntentLauncher.startActivity({
      action: 'android.settings.APPLICATION_DETAILS_SETTINGS',
      data: 'package:' + pkg
    })
  }
}

使用 Expo:
import Constants from 'expo-constants'
import * as IntentLauncher from 'expo-intent-launcher'
const pkg = Constants.manifest.releaseChannel
? Constants.manifest.android.package 
: 'host.exp.exponent'
const openAppSettings = () => {
  if (Platform.OS === 'ios') {
    Linking.openURL('app-settings:')
  } else {
    IntentLauncher.startActivityAsync(
      IntentLauncher.ACTION_APPLICATION_DETAILS_SETTINGS,
      { data: 'package:' + pkg },
    )
  }
}

0
import React, { useCallback } from "react";
import { Button, Linking, StyleSheet, View } from "react-native";

const OpenSettingsButton = ({ children }) => {
  const handlePress = useCallback(async () => {
    // Open the custom settings if the app has one
    await Linking.openSettings();
  }, []);

  return <Button title={children} onPress={handlePress} />;
};

const App = () => {
  return (
    <View style={styles.container}>
      <OpenSettingsButton>Open Settings</OpenSettingsButton>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: "center", alignItems: "center" },
});

0
  import { Linking, Platform } from 'react-native';

   

    const openSettingsAlert = () =>
            Alert.alert('Please provide the require permission from settings', '', [
              {
                text: 'Cancel',
                onPress: () => console.log('Cancel Pressed'),
                style: 'cancel',
              },
              { text: 'Go To Settings', onPress: () => openSettings() },
            ]);



  

    const openSettings = () => {
        if (Platform.OS === 'ios') {
          Linking.openURL('app-settings:');
        } else {
          Linking.openSettings();
        }
      };




    let isStoragePermitted = await requestExternalWritePermission();
            if (isStoragePermitted === true) {
              
            }else{
              console.log('permission not granted');
              openSettingsAlert();
            }







    const requestExternalWritePermission = async () => {
        if (Platform.OS === 'android') {
          try {
            const granted = await PermissionsAndroid.request(
              PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            );
            // If WRITE_EXTERNAL_STORAGE Permission is granted
            return granted === PermissionsAndroid.RESULTS.GRANTED;
          } catch (err) {
            console.warn(err);
            alert('Storage Access permission error:', err);
          }
          return false;
        }
      };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接