android - 如何使用 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 传递给该应用程序以处理实际的下载任务。
解决方案
推荐阅读
- asp-classic - 使用 global.asa 加载 index.asp 时出错
- ansible - 如何有条件地忽略角色遇到的错误
- c - 使用 openmp 的分段错误(核心转储)
- csv - 如何使用 iMacros 重启 CSV - 无限循环?
- javascript - 如何从服务器文件夹访问 React 公用文件夹
- mysql - 如何根据关联表确定计数?
- c# - 用于生成字符串文字以传递给 javascript 函数的语法
- arrays - 如何返回整数字节数组中的最后一个元素
- javascript - Web Audio API - 播放同步声音
- javascript - 数百万 (>5M) 矩形的交互式可视化