如何打开facebook lite app上的页面

时间:2016-10-17 13:07:02

标签: android android-intent

我正在尝试创建一个在Facebook上访问我们的东西,我想在Facebook应用程序上打开页面(如果用户拥有它),如果不在普通浏览器上打开。这是我的代码@Jared Rummler answer

  private void showPage(String url) {
        Uri uri = Uri.parse(url);
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);
            if (applicationInfo.enabled) {
                uri = Uri.parse("fb://facewebmodal/f?href=" + url);
            }
        } catch (PackageManager.NameNotFoundException ignored) {
            try {
                ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.lite", 0);
                if (applicationInfo.enabled) {
                    uri = Uri.parse("fb://facewebmodal/f?href=" + url);
                }
            } catch (PackageManager.NameNotFoundException e) {
                //do nothing
            }
        }

        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }

和url我正在使用字符串:

"https://www.facebook.com/<my_page_name>"

确定。然而,它只适用于正常的Facebook应用程序。任何人都知道我需要改变什么才能使它适用于facebook lite应用程序?

我在logcat上遇到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.technology.softwares.click.rs, PID: 4086                                                 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=fb://facewebmodal/f?href=https://www.facebook.com/<my_page_name>

由于

4 个答案:

答案 0 :(得分:0)

public final void launchFacebook() {
        final String urlFb = "fb://page/"+pageId;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(urlFb));

        // If a Facebook app is installed, use it. Otherwise, launch
        // a browser
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list =
                packageManager.queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
        if (list.size() == 0) {
            final String urlBrowser = "https://www.facebook.com/pages/"+pageId;

            intent.setData(Uri.parse(urlBrowser));
        }

        startActivity(intent);


    }

答案 1 :(得分:0)

尝试插入     intent.setPackage( “com.facebook.mlite”);在你开始之前     活动,例如

private void showPage(String url) {
    Uri uri = Uri.parse(url);
    try {
        ApplicationInfo applicationInfo = 
 getPackageManager().getApplicationInfo("com.facebook.katana", 0);
        if (applicationInfo.enabled) {
            uri = Uri.parse("fb://facewebmodal/f?href=" + url);
        }
    } catch (PackageManager.NameNotFoundException ignored) {
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.lite", 0);
            if (applicationInfo.enabled) {
                uri = Uri.parse("fb://facewebmodal/f?href=" + url);
            }
        } catch (PackageManager.NameNotFoundException e) {
            //do nothing
        }
    }
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.setPackage("com.facebook.mlite");
    startActivity(intent);
}

答案 2 :(得分:0)

重点是获取lite Lite的启动意图。

Uri uri = Uri.parse(url);
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.facebook.lite");
if (intent != null) {
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

答案 3 :(得分:0)

我已经在Kotlin中创建并测试了此方法,以便在Facebook或Lite应用程序中打开直接页面或个人资料。 如果未安装它们,请尝试打开W​​eb浏览器。

private fun openFacebook(url: String) { //this is a normal url for facebook page or group, like "https://www.facebook.com/groups/apkviajando/"

    var uri = Uri.parse(url)
    try {
        val isFacebookInstalled = context!!.existApp("com.facebook.katana") //check if is facebook app installed

        if (isFacebookInstalled)
            uri = Uri.parse("fb://facewebmodal/f?href=$url") //construct facebook Uri

    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }

    val isLiteInstalled = context!!.existApp("com.facebook.lite") //check if is facebook lite installed

    try {

        if (isLiteInstalled) {
            val intent = Intent(Intent.ACTION_VIEW, uri)
            intent.setPackage("com.facebook.lite") //set specific app for facebook lite
            startActivity(intent) //open facebook lite
        }

    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }

    try {

        if (!isLiteInstalled) {
            val intent = Intent(Intent.ACTION_VIEW, uri) //set default uri intent to open in web browser
            startActivity(intent) //open web browser
        }

    } catch (e: ActivityNotFoundException) {
        Toast.makeText(context!!, R.string.facebook_not_found, Toast.LENGTH_SHORT).show() //if there is some error, then show toast
    }

}

fun Context.existApp(appPackage: String): Boolean {

    return try {
        packageManager.getApplicationInfo(appPackage, 0).enabled //check if app is installed and enabled
    } catch (e: Exception) {
        false
    }

}
相关问题