在Android中使用意图无法打开Linkedin个人资料

6
如何使用意图打开linkedin个人资料:
我的代码:
context.getPackageManager().getPackageInfo(APP_PACKAGE_NAME, 0);
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("linkedin://profile/" + profileID));
intent.setPackage(APP_PACKAGE_NAME);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

日志:

12-10 19:18:49.363     936-2104/? I/ActivityManager﹕ START u0 {act=android.intent.action.VIEW dat=linkedin://profile/132778900 pkg=com.linkedin.android cmp=com.linkedin.android/.infra.deeplink.DeepLinkHelperActivity} from pid 8392
12-10 19:18:49.373    1223-1377/? E/﹕ no predefined limited group find for com.linkedin.android, add to foreground group
12-10 19:18:49.463    7964-7964/? I/CrashReporter﹕ Starting activity DeepLinkHelperActivity Intent: Intent { act=android.intent.action.VIEW dat=linkedin://profile/132778900 pkg=com.linkedin.android cmp=com.linkedin.android/.infra.deeplink.DeepLinkHelperActivity }
12-10 19:18:49.493    7964-7964/? E/com.linkedin.android.deeplink.helper.LaunchHelper﹕ urlParams was null, indicating a failure to validate the path in getMap()
12-10 19:18:59.523    7964-7996/? D/LinkedInNetwork﹕ Performing request
12-10 19:19:04.573    1619-1666/? I/XiaomiFirewall﹕ firewall pkgName:com.linkedin.android, result:0

当你说“打开领英个人资料”时,你具体是在哪里以及如何打开它? - gabbar0x
该方法位于另一个类中,我在其中编写打开LinkedIn个人资料的代码,并在单击时调用该方法。 - hharry_tech
我不确定,但您是否尝试将URL与意图一起传递? - gabbar0x
意思是什么?我不明白你在说什么。 - hharry_tech
我已经发布了我正在做的事情。 - hharry_tech
6个回答

6
public void openLinkedInPage(String linkedId) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("linkedin://add/%@" + linkedId));
        final PackageManager packageManager = getContext().getPackageManager();
        final List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (list.isEmpty()) {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.linkedin.com/profile/view?id=" + linkedId));
        }
        startActivity(intent);
    }

3

根据LinkedIn的最新文档,您应该使用DeepLinkHelper来与官方应用程序集成深度链接:

// Get a reference to an activity to return the user to
final Activity thisActivity = this;

// A sample member ID value to open
final String targetID = "abcd1234";

DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance();

// Open the target LinkedIn member's profile
deepLinkHelper.openOtherProfile(thisActivity, targetID, new DeeplinkListener() {
    @Override
    public void onDeepLinkSuccess() {
        // Successfully sent user to LinkedIn app
    }

    @Override
    public void onDeepLinkError(LiDeepLinkError error) {
        // Error sending user to LinkedIn app
    }
});

或者,您可以使用以下方法打开当前已验证用户的个人资料:

// Get a reference to an activity to return the user to
final Activity thisActivity = this;

DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance();

// Open the current user's profile
deepLinkHelper.openCurrentProfile(thisActivity, new DeeplinkListener() {
    @Override
    public void onDeepLinkSuccess() {
        // Successfully sent user to LinkedIn app
    }

    @Override
    public void onDeepLinkError(LiDeepLinkError error) {
        // Error sending user to LinkedIn app
    }
});

无论哪种方式,如果深入研究DeepLinkHelper的实现,你会发现它最终会构造打开领英应用程序的Intent,如下所示:
private void deepLinkToProfile(@NonNull Activity activity, String memberId, @NonNull AccessToken accessToken) {
    Intent i = new Intent("android.intent.action.VIEW");
    Uri.Builder uriBuilder = new Uri.Builder();
    uriBuilder.scheme("linkedin");
    if (CURRENTLY_LOGGED_IN_MEMBER.equals(memberId)) {
        uriBuilder.authority(CURRENTLY_LOGGED_IN_MEMBER);
    } else {
        uriBuilder.authority("profile").appendPath(memberId);
    }
    uriBuilder.appendQueryParameter("accessToken", accessToken.getValue());
    uriBuilder.appendQueryParameter("src", "sdk");
    i.setData(uriBuilder.build());
    Log.i("Url: ", uriBuilder.build().toString());
    activity.startActivityForResult(i, LI_SDK_CROSSLINK_REQUEST_CODE);
} 

正如您所见,除其他事项外,它将访问令牌作为查询参数附加到Uri上,并使用accessToken作为键。添加的另一个查询参数是src,这(大概)只是用于统计目的,而不是实际功能。
现在,如果您查看错误日志中的这行特定代码:
E/com.linkedin.android.deeplink.helper.LaunchHelper﹕ urlParams was null, indicating a failure to validate the path in getMap()

注意: urlParams 为空
我认为它期望数据Uri中有一个或多个查询参数。我倾向于认为这最有可能是指你自己代码片段中缺失的accessToken查询参数,但它是SDK流程的一部分。
希望这能给你正确的方向。最简单的方法可能是使用LinkedIn SDK - 你没有使用的特定原因吗?

现在你有一个不同的问题了吗?看起来你可能需要先通过授权流程(特别是底部的部分)进行操作。请问你能否更新一下你目前拥有的内容,或者如果你的代码已经发生了根本性的变化,请开一个新的问题? - MH.

0
Uri.parse("linkedin://you"));
 final PackageManager packageManager = getContext().getPackageManager();
final List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.isEmpty()) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.linkedin.com/profile/view?id=you"));
}
startActivity(intent);

0
fun getOpenLinkedin(context: Context, url: String) {
    val linkedinIntent = Intent(Intent.ACTION_VIEW)
    linkedinIntent.setClassName("com.linkedin.android", "com.linkedin.android.profile.ViewProfileActivity")
    linkedinIntent.putExtra("memberId", "profile")
    linkedinIntent.setPackage("com.linkedin.android")
    try {
        context.startActivity(linkedinIntent)
    } catch (e: ActivityNotFoundException) {
        context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
    }
}

0
const string PACKAGE = "com.linkedin.android";
Intent intent = new Intent(Intent.ActionView, Uri.Parse("https://www.linkedin.com/in/danielroberts0"));
if(isPackageInstalled(Forms.Context, PACKAGE)) {
    intent.SetPackage(PACKAGE);
}
Forms.Context.StartActivity(intent);

没有任何一个解决方案能够满足我的需求 - 在应用程序中默认或者在网站上打开用户的个人资料页面。

参考: 检查应用程序是否已安装 - Android


0

试试这个,它会在应用程序中打开 LinkedIn 个人资料,如果没有安装 LinkedIn 应用程序,则会在浏览器中打开(截至2018年11月,使用最新版本的 LinkedIn 应用程序),无需使用 DeepLinkHelper

        String profile_url = "https://www.linkedin.com/in/syedfaisal33";
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(profile_url));
            intent.setPackage("com.linkedin.android");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } catch (Exception e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(profile_url)));
        }

如果你想在LinkedIn应用和浏览器之间进行选择,请使用以下代码替换第三行:Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("linkedin://syedfaisal33")); - Syed Faisal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接