Reputation: 69
I have downloaded the Facebook SDK and set it up properly. Everything is working perfect. I can share a link or update my status without a problem. Now I want to do something else. I want to integrate both the Share Intent and the Facebook SDK in such a way that When I click on the share button in my App, the share intent should be called and a list of apps should be shown.
Now if Facebook is selected from the list, it should open up the Facebook SDK share method that I applied on the app without the share intent. If any else app is selected (Gmail/Twitter/SMS etc.) the share intent should call the respective intent normally as it does.
So far, I tried the following code:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"http://somelink.com/somethingelse.php");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList =
pm.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
Log.d("its here:", "goood");
FacebookDialog shareDialog =
new FacebookDialog.ShareDialogBuilder((Activity) v.getContext())
.setLink("http://somelink.com/somethingelse.php").build();
uiHelper.trackPendingDialogCall(shareDialog.present());
final ActivityInfo activity = app.activityInfo;
final ComponentName name =
new ComponentName(activity.applicationInfo.packageName, activity.name);
sharingIntent.addCategory(Intent.CATEGORY_LAUNCHER);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
sharingIntent.setComponent(name);
v.getContext().startActivity(sharingIntent);
break;
} else {
Log.d("shouldnt be here", "nooo");
startActivity(Intent.createChooser(sharingIntent, "Share via"));
break;
}
}
I tried this code but no luck. The Share Intent is called and when I click on Facebook, it opens up a Facebook status update page where as I want it to call the Facebook SDK method that I set up and run through that method with the predefined link.
How can I achieve this?
Upvotes: 3
Views: 637
Reputation: 9223
I use Facebook SDK 4.5.1 and this method works fine with Facebook SDK 4.0+
ShareDialog shareDialog;
CallbackManager callbackManager;
in onCreateView()
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
in your function or code where you want to apply the share dialog.
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Ttile")
.setContentDescription("Description")
.setContentUrl(Uri.parse("http://somelink.com/somethingelse.php"))
.build();
shareDialog.show(linkContent);
}
your onActivityResult() should look like this :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Edit 1: For capturing the event that user selected Facebook from the list of apps.
List<Intent> targetShareIntents=new ArrayList<Intent>();
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/*");
List<ResolveInfo> resInfos=context.getPackageManager().queryIntentActivities(shareIntent, 0);
if(!resInfos.isEmpty()){
for(ResolveInfo resInfo : resInfos){
String packageName=resInfo.activityInfo.packageName;
if(packageName.contains("com.facebook.katana")){
//Facebook sharing code
}
}
if(!targetShareIntents.isEmpty()){
Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
context.startActivity(chooserIntent);
}else{
}
}
Upvotes: 4