You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何通过代码唤起默认启动器选择弹窗引导用户设本应用为默认桌面

前置配置:修改AndroidManifest.xml

首先你需要在Android原生配置文件中注册启动器对应的Intent过滤规则,否则系统不会将你的应用识别为可选的启动器选项:

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <!-- 原有默认的启动入口intent-filter保留 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    <!-- 新增启动器专属intent-filter -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
正确的Flutter调用代码

你现有代码使用的Intent是直接唤起桌面的指令,不是触发默认应用选择弹窗的指令,正确代码如下(可直接复用你已经引入的intent包):

import 'package:intent/intent.dart' as intentFlutter;
import 'package:intent/action.dart' as intentAction;

// 方法放到initState中调用即可
void defaultLauncherOption() {
  // 构造查询所有系统桌面应用的Intent
  intentFlutter.Intent homeIntent = intentFlutter.Intent()
    ..setAction(intentAction.Action.ACTION_MAIN)
    ..addCategory("android.intent.category.HOME");

  // 用选择器包装Intent,唤起系统选择弹窗
  intentFlutter.Intent chooserIntent = intentFlutter.Intent()
    ..setAction(intentAction.Action.ACTION_CHOOSER)
    ..putExtra("android.intent.extra.INTENT", homeIntent)
    ..putExtra("android.intent.extra.TITLE", "选择默认启动器");

  chooserIntent.startActivity().catchError((e) => print("intent error: $e"));
}
高版本Android适配说明
  • Android 10及以上系统如果检测到已经存在默认启动器,可能不会直接弹出选择框,此时可以降级跳转到系统默认应用设置页,代码如下:
void openDefaultAppSettings() {
  intentFlutter.Intent()
    ..setAction("android.settings.MANAGE_DEFAULT_APPS_SETTINGS")
    ..startActivity().catchError((e) => print("settings intent error: $e"));
}
  • 调用前可通过MethodChannel调用原生Android的PackageManager接口,先判断当前应用是否已经是默认启动器,避免重复唤起弹窗。

内容的提问来源于stack exchange,提问作者Patrick Schick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 13:24:03