首页 > 解决方案 > 如何使用 React Native 将下载请求委托给另一个应用程序

问题描述

我正在开发一个带有 expo 的简单应用程序,并使用一个简单的按钮做出原生反应。当我单击该按钮时,它会向 Web 服务发送一个请求,该请求仅返回一个文件的可下载 url。例如“https://somesite.com/video.mp4”

我正在使用 fetch api 将请求发送到服务器。这是我的代码:

import { StatusBar } from "expo-status-bar";
import React from "react";
import { Button, StyleSheet, Text, View } from "react-native";

const handlePress = () => {
  fetch("https://myserver.dev/api")
    .then((response) => response.json())
    .then((responseJson) => alert(responseJson))
    .catch((error) => alert(error));
};

export default function App() {
  return (
    <View style={styles.container}>
      <Button title="Get Download Link" onPress={handlePress} />
      <Text>Open up App.js to start working on your app!</Text>
      <StatusBar style="auto" />
    </View>
  );
}

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

当我运行代码时,它成功提醒我下载网址。但我不想显示此警报,而是将实际下载过程委托给系统中的另一个应用程序。android 编程世界中有一个概念叫做Implicit Intents. 它允许开发人员将任务委托给系统上安装的另一个应用程序。但正是 Android 操作系统让用户可以选择合适的应用程序来执行此任务。我如何在 Expo 和 React Native 中做到这一点。现在这正是我想要做的:获得下载 url 后,系统必须向用户显示适合从 URL 下载的应用程序列表(例如 ADM 或 Chrome)。然后我将从服务器获得的文件 url 传递给该应用程序以处理实际的下载任务。

标签: androidreact-nativefetchexpo

解决方案


推荐阅读