1

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

Community
  • 1
  • 1
EmilyR
  • 179
  • 1
  • 11

4 Answers4

1

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);
}
Xiaoming
  • 11
  • 2
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);


    }
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
0

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);
}
FractalBob
  • 3,225
  • 4
  • 29
  • 40
0

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.

========UPDATED========

The previous response only work for groups pages and URLs like: "https://www.facebook.com/groups/someGroup/"

Based on xiaoming response I have updated my response, and now work for profiles too, with URLs like: "https://www.facebook.com/someProfile/"

private fun openFacebook(url: String) { 

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

        if (isFacebookInstalled) {
            uri = Uri.parse("fb://facewebmodal/f?href=$url") //construct facebook Uri
            val intent = Intent(Intent.ACTION_VIEW, uri)
            intent.setPackage("com.facebook.katana") //set specific app for facebook
            startActivity(intent) //open facebook
            return
        }

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

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

    try {

        if (isLiteInstalled) {

            packageManager.getLaunchIntentForPackage("com.facebook.lite")?.apply {

                action = Intent.ACTION_VIEW
                data = uri
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(this)

            }

            return
        }

    } 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
            return
        }

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

}


private fun existApp(appPackage: String): Boolean {

    return try {
        packageManager.getApplicationInfo(appPackage, 0).enabled
    } catch (e: Exception) {
        false
    }

}
  • This may work for facebook app, but NOT for facebook lite! – makata Feb 14 '20 at 02:12
  • `android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://www.facebook.com/... pkg=com.facebook.lite }` – makata Feb 14 '20 at 02:12