使用MIT App Inventor开发直接打开Flask Web应用的应用

下面我将详细介绍如何创建一个启动后立即打开默认Flask Web应用的安卓应用,无需用户任何操作。

完整开发步骤

1. 创建新项目

  1. 访问 MIT App Inventor
  2. 登录你的Google账号
  3. 点击"Start new project"
  4. 输入项目名称"FlaskWebLauncher"
  5. 点击"OK"

2. 设计用户界面

只需添加以下组件:

  • 1个 WebViewer 组件(改名 webViewer)
    • Width: “Fill parent”
    • Height: “Fill parent”
  • 1个 Notifier 组件(用于错误提示)

说明:因为我们希望应用一启动就打开网页,所以不需要任何按钮或输入框。

3. 添加程序逻辑

点击右上角"Blocks"按钮切换到编程视图:

// 当屏幕初始化时
when Screen1.Initialize do
  // 设置你的Flask应用URL
  set webViewer.HomeUrl to "http://your-flask-app.com"  // 替换为你的实际URL
  
  // 立即加载网页
  call webViewer.GoHome
  
  // 可选:设置加载状态
  set Screen1.Title to "正在加载Flask应用..."

4. 添加错误处理

// 当页面加载完成时
when webViewer.AfterPageLoad url: currentUrl do
  set Screen1.Title to "我的Flask应用"

// 当发生错误时
when webViewer.ErrorOccurred errorCode: code description: description do
  call notifier.ShowAlert text: join "加载错误: " description
  set Screen1.Title to "加载失败"

5. 配置屏幕属性(重要)

在Designer视图中选择Screen1,设置以下属性:

  • Title:我的Flask应用
  • Scrollable:取消勾选(因为我们使用WebViewer全屏)
  • ScreenOrientation:根据你的Flask应用选择:
    • portrait(竖屏)
    • landscape(横屏)
    • sensor(自动旋转)

6. 设置应用图标(可选)

  1. 准备一个512×512像素的PNG图标
  2. 在Screen1属性中找到"Icon"
  3. 点击上传你的图标文件

高级优化版本

如果你想添加更多功能,可以使用这个增强版:

// 全局变量
global flaskUrl
global isLoading

// 屏幕初始化
when Screen1.Initialize do
  set global flaskUrl to "http://your-flask-app.com"  // 你的Flask地址
  set global isLoading to true
  
  // 全屏模式(可选)
  set Screen1.ActionBar to false
  
  // 开始加载
  set webViewer.HomeUrl to global flaskUrl
  call webViewer.GoHome
  set Screen1.Title to "正在加载..."

// 页面加载成功
when webViewer.AfterPageLoad url: currentUrl do
  set global isLoading to false
  set Screen1.Title to "我的应用"
  
  // 隐藏状态栏(可选)
  call Screen1.HideStatusBar

// 页面加载错误
when webViewer.ErrorOccurred errorCode: code description: desc do
  set global isLoading to false
  call notifier.ShowChooseDialog 
    title: "加载失败" 
    message: join "无法加载: " desc 
    button1Text: "重试" 
    button2Text: "退出"
  
// 处理对话框选择
when notifier.AfterChoosing choice: choice do
  if (choice = "重试") then
    set global isLoading to true
    call webViewer.GoHome
  else if (choice = "退出") then
    close application
  end if

// 返回键处理(可选)
when Screen1.BackPressed do
  if (webViewer.CanGoBack and not global isLoading) then
    call webViewer.GoBack
    prevent back
  end if

打包APK文件

  1. 点击顶部菜单"Build"
  2. 选择"App (save .apk to my computer)"
  3. 等待编译完成(约1-2分钟)
  4. 下载生成的APK文件
  5. 传输到安卓手机安装

测试建议

  1. 使用AI Companion实时测试:

    • 安装MIT AI2 Companion应用
    • 扫码连接实时测试
  2. 测试不同网络环境:

    • WiFi和移动数据网络
    • 检查离线情况下的表现
  3. 测试不同设备:

    • 多种屏幕尺寸
    • 不同安卓版本

注意事项

  1. URL协议:确保你的URL以http://或https://开头

  2. 网络权限:MIT App Inventor会自动添加互联网权限

  3. Flask应用配置:

    # Flask应用应该允许跨域访问
    from flask_cors import CORS
    app = Flask(__name__)
    CORS(app)
    
  4. 本地测试:

    • 如果测试本地Flask开发服务器:
    • 使用电脑的本地IP(如http://192.168.x.x:5000)
    • 确保手机和电脑在同一网络

这个应用将作为一个简单的WebView容器,启动后立即全屏显示你的Flask Web应用,适合将Web应用快速打包为原生安卓应用。

在这里插入图片描述

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐