如何在react native android中打开应用程序设置页面?

qaxu7uf2  于 2022-12-04  发布在  React
关注(0)|答案(8)|浏览(239)

最后,react native bridge是唯一的方法。但是有没有任何npm包已经成功地为任何人工作过呢?

brccelvz

brccelvz1#

您可以使用链接库打开应用程序设置。

import {Linking} from 'react-native';

Linking.openSettings();

公文:https://reactnative.dev/docs/linking#opensettings

zpf6vheq

zpf6vheq2#

您可以使用@react-native-community/react-native-permissions库。
此处为官方文件:https://github.com/react-native-community/react-native-permissions#opensettings

范例:

import { openSettings } from 'react-native-permissions';
openSettings();
xdyibdwo

xdyibdwo3#

结合现有答案:这对我来说适用于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()
qc6wkl3g

qc6wkl3g4#

这是一个完整的答案都android和iOS.
无世博会:

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
    })
  }
}

与世博会:

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 },
    )
  }
}
neekobn8

neekobn85#

用途

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'
})
  • 全球定位系统 *
IntentLauncher.startActivity({
 action: 'android.settings.LOCATION_SOURCE_SETTINGS'
})

内部监督系统

无线网络

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

全球定位系统

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

kpbwa7wx6#

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" },
});
ttvkxqim

ttvkxqim7#

从“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;
    }
  };
wqnecbli

wqnecbli8#

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

注意-您可以发送任何Android可接受的自定义操作,此外,您还可以将数据传递给Intent。
以下是打开设置的示例:

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');

希望这对你或别人有帮助。谢谢!
快乐编码:-)

相关问题