Skip to content

Instantly share code, notes, and snippets.

@clzola
Created September 16, 2017 19:48
Show Gist options
  • Save clzola/65bfa196bcbc601c2089fe8d74d3cd63 to your computer and use it in GitHub Desktop.
Save clzola/65bfa196bcbc601c2089fe8d74d3cd63 to your computer and use it in GitHub Desktop.
IntentUtilities fb, insta, email
public class IntentUtilities {
/**
* <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
* default web browser will be used.</p>
*
* https://stackoverflow.com/questions/4810803/open-facebook-page-from-android-app
*
* <p>Example usage:</p>
*
* {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
*
* @param pm The {@link PackageManager}. You can find this class through {@link Context#getPackageManager()}.
* @param url The full URL to the Facebook page or profile.
* @return An intent that will open the Facebook page/profile.
*/
public static Intent newFacebookIntent(PackageManager pm, String url) {
Uri uri = Uri.parse(url);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
// http://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
// https://www.instagram.com/gdjedanas
public static Intent newInstagramIntent(PackageManager pm, String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.instagram.android", 0);
if(applicationInfo.enabled) {
intent.setPackage("com.instagram.android");
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return intent;
}
public static Intent newWebsiteIntent(String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
return intent;
}
public static Intent newEmailIntent(String to, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
if(subject != null)
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
return intent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment