1

Following code works perfectly,

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
startActivity(Intent.createChooser(sharingIntent, "Share using"));

but it open a list and from this list I have to select Facebook and then everything is smooth.

But is there any config changes that can open Facebook sharing directly instead of going through the menu.

user3141985
  • 1,395
  • 4
  • 17
  • 36

2 Answers2

4

Try the following code, now the intent will open Facebook directly.

List<Intent> targetedShareIntents = new ArrayList<Intent>();

Intent facebookIntent = getShareIntent("facebook", "subject", "text_or_url");
// subject may not work, but if you have a url place it in text_or_url
if(facebookIntent != null)
    targetedShareIntents.add(facebookIntent);

Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");

chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

startActivity(chooser);

// To precisely get the facebook intent use the following code

private Intent getShareIntent(String type, String subject, String text) 
{
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
    System.out.println("resinfo: " + resInfo);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                    info.activityInfo.name.toLowerCase().contains(type) ) {
                share.putExtra(Intent.EXTRA_SUBJECT,  subject);
                share.putExtra(Intent.EXTRA_TEXT,     text);
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return null;

        return share;
    }
    return null;
}

Above code is extracted from this stackoverflow-post

Community
  • 1
  • 1
MTahir
  • 1,173
  • 1
  • 13
  • 25
  • It works fine when I put a try catch block in onCreate part of the activity, but I am getting `Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@417c15b0 that was originally registered here. Are you missing a call to unregisterReceiver()?` error – user3141985 Apr 16 '14 at 17:14
1

this should be possible, if you get first all activities, that supports your intent and then get the correct and do a explicit call. To get all activities use context.getPackageManager.queryIntentActivities(Intent, int). The coding shouldn't be a big problem.

Check this link: PackageManager docu

speedy1034
  • 304
  • 2
  • 9
  • I am not an expert, but do you mean that I have to iterate through the list of all items displayed, but will it then show only Facebook in that list and then click Facebook again ? – user3141985 Apr 16 '14 at 17:12
  • No you iterate and look for the fb intent and call this explicit. – speedy1034 Apr 16 '14 at 20:35