【问题标题】:How to show a chooser dialog even if the URI is an App Links one即使 URI 是应用程序链接,如何显示选择器对话框
【发布时间】:2020-09-17 12:38:52
【问题描述】:

The reference 关于android.content.Intent.ACTION_CHOOSER 说:

…所有可能的活动将始终显示,即使其中一项当前被标记为首选活动。

The one 关于 App Links 说:

没有对话框;您的应用会打开以处理您的网站链接

我认为这些是自相矛盾的。

例如,应用链接 URI 的以下选择器是否显示对话框?在我的测试中,没有显示任何对话框。

package com.example.applinkchooser;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri uri = Uri.parse(<an app links uri>);

        Intent uriIntent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(Intent.createChooser(uriIntent, null));

        finish();
    }
}

那么我的问题是如何显示选择器对话框,即使 URI 是 App Links 之一。我必须使用android.content.pm.PackageManager吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    如果 SDK 是 M 或更高版本,则必须在 flag 中添加 MATCH_ALL。

    int appsFlag = PackageManager.MATCH_DEFAULT_ONLY;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        appsFlag = PackageManager.MATCH_ALL;
    }
    
    PackageManager pm = getPackageManager();
    Intent urlIntent = new Intent(Intent.ACTION_VIEW, uri);
    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(urlIntent, appsFlag);
    

    【讨论】:

    • 没有应用链接的应用即使采用此解决方案也不会显示。
    猜你喜欢
    • 2014-06-12
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多