I need to develop an app that has the share function. I have to share on Facebook, twitter, email and maybe other services.
How can I do this? There a library on the net? For the iOS development there were ShareKit, but for Android?
Thanks :)
I need to develop an app that has the share function. I have to share on Facebook, twitter, email and maybe other services.
How can I do this? There a library on the net? For the iOS development there were ShareKit, but for Android?
Thanks :)
yes you can ... you just need to know the exact package name of the application:
And you can create the intent like this
Intent intent = context.getPackageManager().getLaunchIntentForPackage(application);
if (intent != null) {
// The application exists
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage(application);
shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, description);
// Start the specific social application
context.startActivity(shareIntent);
} else {
// The application does not exist
// Open GooglePlay or use the default system picker
}
The ACTION_SEND will work correctly for all and it takes the Body of the text to wall in twitter, G mail.. but it fails For Face Book page... Its as known bug in the Facebook android SDK .. but still they haven't fixed it
This will help
1- First Define This Constants
public static final String FACEBOOK_PACKAGE_NAME = "com.facebook.katana";
public static final String TWITTER_PACKAGE_NAME = "com.twitter.android";
public static final String INSTAGRAM_PACKAGE_NAME = "com.instagram.android";
public static final String PINTEREST_PACKAGE_NAME = "com.pinterest";
public static final String WHATS_PACKAGE_NAME = "com.whatsapp";
2- Second Use This method
public static void shareAppWithSocial(Context context, String application, String title,
String description) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setPackage(application);
intent.putExtra(android.content.Intent.EXTRA_TITLE, title);
intent.putExtra(Intent.EXTRA_TEXT, description);
intent.setType("text/plain");
try {
// Start the specific social application
context.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
// The application does not exist
Toast.makeText(context, "app have not been installed.", Toast.LENGTH_SHORT).show();
}
}
Create Choose Editable
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
String sAux = "\nLet me recommend you this application\n\n";
sAux = sAux + "https://play.google.com/store/apps/details?id=the.package.id \n\n";
sendIntent.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(sendIntent, "choose one"));
==============================================================
Create Choose Default
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
startActivity(sendIntent);
=================================================================
Multi-Piece send
Send multiple pieces of content
To share multiple pieces of content, use the ACTION_SEND_MULTIPLE action together with a list of URIs pointing to the content. The MIME type varies according to the mix of content you're sharing. For example, if you share 3 JPEG images, the type is still "image/jpeg". For a mixture of image types, it should be "image/*" to match an activity that handles any type of image. You should only use "*/*" if you're sharing out a wide variety of types. As previously stated, it's up to the receiving application to parse and process your data. Here's an example:
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
================================================================
Share With Facebook
ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
.setQuote("Application of social rating share with your friend")
.setContentUrl(Uri.parse("https://google.com"))
.build();
if (ShareDialog.canShow(ShareLinkContent.class)) {
sd.show(shareLinkContent);
}
==============================================================
Share With SMS
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("Body", "Application of social rating share with your friend");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
==============================================================
Share With Mail
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "Address");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Application of social rating share with your friend");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send Email..."));
=============================================================
Share with WhatsApp
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Application of social rating share with your friend");
try {
Objects.requireNonNull(getActivity()).startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "WhatsApp have not been installed.", Toast.LENGTH_SHORT).show();
}
String message = "This is testing."
Intent shareText = new Intent(Intent.ACTION_SEND);
shareText .setType("text/plain");
shareText .putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareText , "Title of the dialog the system will open"));
sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"your subject" );
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "your text");
startActivity(Intent.createChooser(sharingIntent, ""));
Share on Facebook
private ShareDialog shareDialog;
shareDialog = new ShareDialog((AppCompatActivity) context);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
Log.e("TAG","Facebook Share Success");
logoutFacebook();
}
@Override
public void onCancel() {
Log.e("TAG","Facebook Sharing Cancelled by User");
}
@Override
public void onError(FacebookException error) {
Log.e("TAG","Facebook Share Success: Error: " + error.getLocalizedMessage());
}
});
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setQuote("Content goes here")
.setContentUrl(Uri.parse("URL goes here"))
.build();
shareDialog.show(linkContent);
}
AndroidManifest.xml
<!-- Facebook Share -->
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider{@string/facebook_app_id}"
android:exported="true" />
<!-- Facebook Share -->
dependencies {
implementation 'com.facebook.android:facebook-share:[5,6)' //Facebook Share
}
Share on Twitter
public void shareProductOnTwitter() {
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(getResources().getString(R.string.twitter_api_key), getResources().getString(R.string.twitter_api_secret)))
.debug(true)
.build();
Twitter.initialize(config);
twitterAuthClient = new TwitterAuthClient();
TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
if (twitterSession == null) {
twitterAuthClient.authorize(this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
TwitterSession twitterSession = result.data;
shareOnTwitter();
}
@Override
public void failure(TwitterException e) {
Log.e("TAG","Twitter Error while authorize user " + e.getMessage());
}
});
} else {
shareOnTwitter();
}
}
private void shareOnTwitter() {
StatusesService statusesService = TwitterCore.getInstance().getApiClient().getStatusesService();
Call<Tweet> tweetCall = statusesService.update("Content goes here", null, false, null, null, null, false, false, null);
tweetCall.enqueue(new Callback<Tweet>() {
@Override
public void success(Result<Tweet> result) {
Log.e("TAG","Twitter Share Success");
logoutTwitter();
}
@Override
public void failure(TwitterException exception) {
Log.e("TAG","Twitter Share Failed with Error: " + exception.getLocalizedMessage());
}
});
}
Addition to the answer given by Ionut Negru, From Android 11 don't forget to add
<queries>
....
<package android:name="com.whatsapp" />
<package android:name="com.whatsapp.w4b" />
<package android:name="com.twitter.android" />
<package android:name="com.facebook.katana" />
</queries>