Created
November 5, 2014 05:54
-
-
Save gotokatsuya/e4f569ea18a1eb9baee0 to your computer and use it in GitHub Desktop.
ShareHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by katsuyagoto. | |
*/ | |
public class ShareHelper { | |
public static final int REQUEST_FACEBOOK = 1; | |
public static final int REQUEST_TWITTER = 2; | |
public static final int REQUEST_LINE = 3; | |
public static final int REQUEST_SMS = 4; | |
public static final int REQUEST_MAIL = 5; | |
public static void shareTwitter(String message, Activity activity) { | |
if (!appInstalled(activity, activity.getString(R.string.twitter_package))) { | |
ToastHelper.shortMessage(activity, activity.getString(R.string.twitter_not_installed)); | |
} else { | |
try { | |
String shareString = URLEncoder.encode(message, "UTF-8"); | |
String url = "http://twitter.com/share?text=" + shareString; | |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
activity.startActivityForResult(intent, REQUEST_TWITTER); | |
} catch (Exception e) { | |
} | |
} | |
} | |
public static void shareFacebook(String message, Activity activity) { | |
if (!appInstalled(activity, activity.getString(R.string.facebook_package))) { | |
ToastHelper.shortMessage(activity, activity.getString(R.string.facebook_not_installed)); | |
} else { | |
try { | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_SEND); | |
intent.setPackage(activity.getString(R.string.facebook_package)); | |
intent.setType("text/plain"); | |
intent.putExtra(Intent.EXTRA_TEXT, message); | |
activity.startActivityForResult(intent, REQUEST_FACEBOOK); | |
} catch (Exception e) { | |
} | |
} | |
} | |
public static void shareLine(String message, Activity activity) { | |
if (!appInstalled(activity, activity.getString(R.string.line_package))) { | |
ToastHelper.shortMessage(activity, | |
activity.getString(R.string.line_not_installed)); | |
} else { | |
try { | |
String lineString = "line://msg/text/" | |
+ URLEncoder.encode(message, "UTF-8"); | |
Intent intent = Intent.parseUri(lineString, Intent.URI_INTENT_SCHEME); | |
activity.startActivityForResult(intent, REQUEST_LINE); | |
} catch (Exception e) { | |
} | |
} | |
} | |
public static void shareSMS(String message, FragmentActivity activity) { | |
try { | |
Uri smsNumber = Uri.parse("smsto:"); | |
Intent intent = new Intent(Intent.ACTION_SENDTO, smsNumber); | |
intent.putExtra("sms_body", message); | |
activity.startActivityForResult(intent, REQUEST_SMS); | |
} catch (ActivityNotFoundException e) { | |
ToastHelper.shortMessage(activity, | |
activity.getString(R.string.sms_not_installed)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void shareMail(String message, FragmentActivity activity) { | |
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); | |
emailIntent.setType("text/html"); | |
emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name)); | |
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(message.replaceAll("\n", "<br>"))); | |
activity.startActivityForResult(Intent.createChooser(emailIntent, "Email:"), REQUEST_MAIL); | |
} | |
public static boolean appInstalled(Context context, String uri) { | |
PackageManager pm = context.getPackageManager(); | |
try { | |
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); | |
return true; | |
} catch (PackageManager.NameNotFoundException e) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment