How open a page on facebook lite app

心已入冬 提交于 2020-01-23 22:07:41

问题


I'm trying to create a Visit us on facebook thing, I want to rather open the page on facebook app (if the user have it) if don't then open on normal browser. This is my code based in @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));
    }

and to url I'm using the string:

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

Ok. However it just only works to normal facebook app. Anyone know what I have to change to make it works with facebook lite app?

I'm catching the following error on 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>

Thanks


回答1:


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


    }



回答2:


Try inserting intent.setPackage("com.facebook.mlite"); immediately before starting your Activity, e.g.,

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



回答3:


The point is get facebook lite launch intent.

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



回答4:


I have created and tested this method in Kotlin to open direct page or profile in Facebook, or Lite app. If none of them are installed, then try to open a web browser.

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
    }

}


来源:https://stackoverflow.com/questions/40087231/how-open-a-page-on-facebook-lite-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!