Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Last active September 4, 2021 16:24
Show Gist options
  • Save jaisonfdo/47249e335819e37882c143591eca1a61 to your computer and use it in GitHub Desktop.
Save jaisonfdo/47249e335819e37882c143591eca1a61 to your computer and use it in GitHub Desktop.
Connect social media account in a single line of code in Android.For more information, check out my detailed guide here : http://droidmentor.com/connect-social-media-account/
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;
/**
* Created by Jaison
*/
public class SocialConnection {
public static String networkErrorMessage = "Network not available";
public static boolean checkInternetConnection = true;
public static boolean showErrorMessage = true;
/**
* Connect Facebook Account (Page/Profile)
*
* @param context
* @param fbID
*/
public static void connectFacebook(Context context, String fbID) {
Intent intent = getIntent();
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.facebook.katana")) {
intent.setPackage("com.facebook.katana");
int versionCode = 0;
try {
versionCode = context.getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (versionCode >= 3002850) {
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + "http://m.facebook.com/" + fbID);
intent.setData(uri);
} else {
Uri uri = Uri.parse("fb://profile/" + fbID);
intent.setData(uri);
}
} else {
intent.setData(Uri.parse("http://m.facebook.com/" + fbID));
}
context.startActivity(intent);
}
}
/**
* Connect Twitter Account
*
* @param context
* @param handle
*/
public static void connectTwitter(Context context, String handle) {
Intent intent = getIntent();
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.twitter.android")) {
intent.setPackage("com.twitter.android");
intent.setData(Uri.parse("twitter://user?screen_name=" + handle));
} else {
intent.setData(Uri.parse("https://twitter.com/" + handle));
}
context.startActivity(intent);
}
}
/**
* Connect Instagram
*
* @param context
* @param id
*/
public static void connectInstagram(Context context, String id) {
Intent intent = getIntent();
intent.setData(Uri.parse("http://instagram.com/_u/" + id));
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.instagram.android"))
intent.setPackage("com.instagram.android");
context.startActivity(intent);
}
}
/**
* Send Email
*
* @param context
* @param mailID
*/
public static void sendMail(Context context, String mailID[]) {
sendMail(context, mailID, "");
}
/**
* Send Email
*
* @param context
* @param mailID
* @param subject
*/
public static void sendMail(Context context, String mailID[], String subject) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, mailID);
if (!TextUtils.isEmpty(subject))
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.google.android.gm"))
intent.setPackage("com.google.android.gm");
context.startActivity(intent);
}
}
/**
* Connect LinkedIn Profile
*
* @param context
* @param id
*/
public static void connectLinkedIn(Context context, String id) {
Intent intent = getIntent();
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.linkedin.android")) {
intent.setPackage("com.linkedin.android");
intent.setData(Uri.parse("linkedin://profile/" + id));
} else
intent.setData(Uri.parse("https://www.linkedin.com/in/" + id));
context.startActivity(intent);
}
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "app package name")) {
intent.setPackage("app package name");
intent.setData(Uri.parse("page URI" + id));
} else
intent.setData(Uri.parse("URL" + id));
context.startActivity(intent);
}
}
/**
* Connect YouTube
*
* @param context
* @param channel
*/
public static void connectYouTube(Context context, String channel) {
Intent intent = getIntent();
intent.setData(Uri.parse("http://youtube.com/channel/" + channel));
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.google.android.youtube"))
intent.setPackage("com.google.android.youtube");
context.startActivity(intent);
}
}
/**
* Link GitHub Profile
*
* @param context
* @param id
*/
public static void connectGitHub(Context context, String id) {
Intent intent = getIntent();
intent.setData(Uri.parse("https://github.com/" + id));
if (isNetworkAvailable(context))
context.startActivity(intent);
}
/**
* Redirect to PlayStore App Page
*
* @param context
* @param appPkgName
*/
public static void connectPlayStoreApp(Context context, String appPkgName) {
Intent intent = getIntent();
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.android.vending")) {
intent.setPackage("com.android.vending");
intent.setData(Uri.parse("market://details?id=" + appPkgName));
} else
intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + appPkgName));
context.startActivity(intent);
}
}
/**
* ListOut Apps of the developer
*
* @param context
* @param devName
*/
public static void listPlayStoreApps(Context context, String devName) {
Intent intent = getIntent();
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.android.vending")) {
intent.setPackage("com.android.vending");
intent.setData(Uri.parse("market://search?q=" + devName));
} else
intent.setData(Uri.parse("http://play.google.com/store/apps/developer?id=" + devName));
context.startActivity(intent);
}
}
/**
* Show URL
*
* @param context
* @param url
*/
public static void loadURL(Context context, String url) {
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
if (isNetworkAvailable(context)) {
Intent intent = getIntent();
intent.setData(Uri.parse(url));
context.startActivity(intent);
}
}
private static Intent getIntent() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
/**
* Check the given App is Available (or) Not
*
* @param context
* @param appName
* @return
*/
public static Boolean isAppAvailable(Context context, String appName) {
PackageManager pm = context.getPackageManager();
boolean isInstalled;
try {
pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
isInstalled = true;
} catch (PackageManager.NameNotFoundException e) {
isInstalled = false;
}
return isInstalled;
}
/**
* Check the Internet Connection
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
if (checkInternetConnection) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else {
if (showErrorMessage)
Toast.makeText(context, networkErrorMessage, Toast.LENGTH_SHORT).show();
return false;
}
} else
return true;
}
public static void setNetworkErrorMessage(String networkErrorMessage) {
SocialConnection.networkErrorMessage = networkErrorMessage;
}
public static void setCheckInternetConnection(boolean checkInternetConnection) {
SocialConnection.checkInternetConnection = checkInternetConnection;
}
public static void setShowErrorMessage(boolean showErrorMessage) {
SocialConnection.showErrorMessage = showErrorMessage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment