无法使用 android 中的意图打开 Linkedin 个人资料

Unable to open Linkedin profile using intent in android

如何使用 intent 打开 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
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);

基于最新的 documentation from 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
    }
});

无论哪种方式,如果深入 implementation of the DeepLinkHelper,您会发现它最终构建了 Intent 打开 LinkedIn 应用程序,如下所示:

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 was null

对我来说,这听起来像是需要一个或多个数据查询参数 Uri。我倾向于说这很可能指的是(至少)您自己的代码片段中缺少的 accessToken 查询参数,但它是 SDK 流程的一部分。

希望这是朝着正确方向的推动。仅使用 LinkedIn SDK 可能是最简单的 - 您有什么特殊原因不这样做吗?

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);

None 的解决方案提供了我所需要的 - 在应用程序中打开用户个人资料页面。默认情况下或其他网站。

参考: Check if application is installed - Android

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);
    }

试试这个,它将在应用程序中打开 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)));
        }
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)))
    }
}