【问题标题】:Issue in opening Facebook page via Facebook Application instead of Browser通过 Facebook 应用程序而不是浏览器打开 Facebook 页面的问题
【发布时间】:2017-07-07 08:47:42
【问题描述】:

我需要在我安装的 Facebook 应用程序中打开 Facebook 页面。我为此调用了以下函数:

private void loadFacebookUrl() {
    if (Constant.isOnline(AboutActivity.this)) {
        try {
            if (appInstalledOrNot(AboutActivity.this, "com.facebook.katana")) {
                this.getPackageManager().getPackageInfo("com.facebook.katana", 0);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/MyPage/")).setPackage("com.facebook.katana"));
            } else {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Constant.displayToast(AboutActivity.this, getResources().getString(R.string.msg_internet));
    }
}

但是,我的 Logcat 中出现以下异常:

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://www.facebook.com/... pkg=com.facebook.katana }
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3978)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3939)
07-07 14:19:24.070 5094-5094/com.app.thelist W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
07-07 14:19:24.071 5094-5094/com.app.thelist W/System.err:     at android.app.Activity.startActivity(Activity.java:4262)
07-07 14:19:24.071 5094-5094/com.app.thelist W/System.err:     at android.app.Activity.startActivity(Activity.java:4230)
07-07 14:19:24.071 5094-5094/com.app.thelist W/System.err:     at com.app.thelist.activity.AboutActivity.loadFacebookUrl(AboutActivity.java:98)
07-07 14:19:24.071 5094-5094/com.app.thelist W/System.err:     at com.app.thelist.activity.AboutActivity.onClick(AboutActivity.java:76)

我应该怎么做才能完成任务?谢谢。

【问题讨论】:

    标签: android facebook android-intent uri android-facebook


    【解决方案1】:

    您的问题在于链接格式...您应该使用:fb://facewebmodal/f?href=

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=https://www.facebook.com/mypage"));
    

    检查他是否有应用,然后使用上面的url格式,否则将他重定向到浏览器:

    String pageurl = "https://www.facebook.com/mypage";
    boolean hasFacebook = appInstalledOrNot(AboutActivity.this, "com.facebook.katana");
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse((hasFacebook?("fb://facewebmodal/f?href="):"") + pageurl));
    startActivity(i);
    

    【讨论】:

    • 它可以工作,但当应用程序被禁用时就不行了,知道吗?
    • 上述代码检查用户是否拥有该应用,然后将您的页面 url 发送到 fb 应用。如果它不可用,那么它会将 url 发送到网络浏览器。
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2022-10-12
    • 2016-01-10
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多