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

点击SwitchPreferenceCompat时如何唤起系统默认浏览器选择设置界面?

实现点击SwitchPreferenceCompat唤起默认浏览器设置的完整方案

步骤1:修改SettingsFragment代码

替换你现有的SettingsFragment代码为以下版本,包含状态校验、系统界面唤起、状态同步逻辑:

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.prefs, rootKey)
        // 初始化时同步默认浏览器开关状态
        syncDefaultBrowserSwitchState()
    }

    override fun onResume() {
        super.onResume()
        // 从系统设置页返回后同步开关状态
        syncDefaultBrowserSwitchState()
    }

    override fun onPreferenceTreeClick(preference: Preference?): Boolean {
        when (preference?.key) {
            "default_browser" -> {
                val context = requireContext()
                when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
                        // Android 12及以上直接跳转默认应用管理页
                        val intent = Intent(Settings.ACTION_MANAGE_DEFAULT_APP).apply {
                            putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                            putExtra(Settings.EXTRA_ROLE_NAME, RoleManager.ROLE_BROWSER)
                        }
                        startActivity(intent)
                    }
                    else -> {
                        // 低版本通过http请求唤起默认浏览器选择弹窗
                        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")).apply {
                            addCategory(Intent.CATEGORY_BROWSABLE)
                        }
                        startActivity(Intent.createChooser(intent, "选择默认浏览器"))
                    }
                }
                return true
            }
        }
        return super.onPreferenceTreeClick(preference)
    }

    /**
     * 校验当前应用是否为系统默认浏览器
     */
    private fun isMyAppDefaultBrowser(): Boolean {
        val testIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com"))
        val resolveInfo = requireContext().packageManager.resolveActivity(
            testIntent,
            PackageManager.MATCH_DEFAULT_ONLY
        )
        return resolveInfo?.activityInfo?.packageName == requireContext().packageName
    }

    /**
     * 同步开关状态和实际默认浏览器状态一致
     */
    private fun syncDefaultBrowserSwitchState() {
        val defaultBrowserPref = findPreference<SwitchPreferenceCompat>("default_browser")
        defaultBrowserPref?.isChecked = isMyAppDefaultBrowser()
    }
}

步骤2:补充Manifest配置

2.1 给浏览器处理页面添加intent-filter

确保你的应用中负责打开网页的Activity添加了如下配置,系统才会将你的应用识别为浏览器候选:

<activity android:name=".YourBrowserActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" />
        <data android:scheme="https" />
    </intent-filter>
</activity>

2.2 适配包可见性限制(Android 11+)

在AndroidManifest.xml的根节点下添加如下配置,避免校验默认浏览器时无法拿到正确的系统解析结果:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
</queries>

注意事项

  • 开关状态会自动和实际系统默认浏览器状态同步,无需手动修改SwitchPreferenceCompat的选中状态
  • Android 12及以上会直接跳转专门的默认浏览器设置页,用户体验更统一

内容的提问来源于stack exchange,提问作者Rizwan Mazumder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 21:36:08