【问题标题】:Force to open link in browser for fallback强制在浏览器中打开链接以进行后备
【发布时间】:2016-05-31 19:28:50
【问题描述】:

我有一个意图过滤器,可以打开主机“xyzxyz.com/value”的所有链接。

在清单中我设置了一个 Activity 来接收它:

 <activity
        android:name=".ui.LinkOpenerActivity"
        android:label="@string/app_name" >
        <intent-filter android:label="@string/app_name">
            <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"
                  android:host="xyzxyz.com" />
        </intent-filter>
    </activity>

因此,当用户单击“xyzxyz.com/value”链接时,它会打开应用程序,该应用程序会根据 URL 中的“值”执行某些操作。

我想做的是打开默认浏览器,以防“值”的值不正确。

我试过了

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlReceivedFromIntent));

startActivity(browserIntent);

但它最终进入无限循环,因为它再次打开我的应用程序...有什么建议可以在默认浏览器中回退并在其中打开 http://xyzxyz.com/value 吗?


使用 Intent 选择器解决:

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(
            Intent.createChooser(browserIntent, null)
    );
    finish();

【问题讨论】:

    标签: android


    【解决方案1】:

    通过使用context.getPackageManager().queryIntentActivities,您可以获得能够解决您的意图的活动列表。然后,您可以使用 Intent.setClassName 过滤掉您的并明确选择另一个(或使用 Intent.createChooser 强制选择器)

    【讨论】:

    • 如果您设置了默认意图,那么 queryIntentActivities 将不会返回 Marshmallow 及以上的所有活动列表。 API 中的错误
    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 2018-02-16
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多