通过Facebook应用程序而不是浏览器打开Facebook页面的问题

时间:2017-07-07 08:47:42

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

我需要在已安装的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)

完成任务需要做些什么?感谢。

1 个答案:

答案 0 :(得分:0)

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

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

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

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