28

Now i can use the share intent to open the share dialog

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, link);  
    startActivity(Intent.createChooser(intent, "Share with"));

but i need the dialog not to appear and share directly to one social network for example (FB or twitter)

any advice how to do that ?

Zak
  • 571
  • 2
  • 6
  • 13
  • 1
    Then why don't you implement directly FB status update using Facebook SDK or twitter status update using Twitter API? – Paresh Mayani Jul 26 '11 at 08:51
  • 1
    i want only to share links on FB or twitter ,, do i have to create FB application and use it's key of i can share links directly ? – Zak Jul 26 '11 at 09:02

4 Answers4

84

There is a way to directly open the intent you wants. You can get the list of intents and open only one.

See this code:

private void initShareIntent(String type) {
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    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,     "your text");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) ); // Optional, just if you wanna share an image.
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return;

        startActivity(Intent.createChooser(share, "Select"));
    }
}

If you wanna open twitter, do that:

initShareIntent("twi");

if facebook:

initShareIntent("face");

if mail:

initShareIntent("mail"); // or "gmail"

If you wanna show a list of intents that match with the type, insted of use the first mach, see this post: Android Intent for Twitter application

Community
  • 1
  • 1
Derzu
  • 7,011
  • 3
  • 57
  • 60
  • @Derzu If i want to open Google+ and Email(Not gmail i.e the mail client from where i can mail to yahoo, hotmail etc) – AndroidDev Sep 03 '12 at 06:50
  • @Anshuman I'm not sure if I understood your question. If you wanna ask the user to choose the e-mail application do like is explained in this post: http://stackoverflow.com/questions/2077008/android-intent-for-twitter-application/9151983#9151983 – Derzu Sep 03 '12 at 15:01
  • 1
    @SudiptaforAndroid Do you have the facebook application installed? The facebook app intent does not accept the EXTRA_TEXT parameter neither the EXTRA_SUBJECT. But the imagem will be attached. – Derzu Nov 27 '12 at 13:17
  • yes..i have the FB installed. I am able to share any url to FB, but its not sharing plain text ...can u please help.. – Sudipta Som Nov 28 '12 at 08:56
  • @SudiptaforAndroid It is a known bug in the official facebook app. You can solve this problem by not using Intent, use de facebooksdk. See these links: http://stackoverflow.com/questions/8771333/android-share-intent-for-facebook-share-text-and-link and http://stackoverflow.com/questions/3481079/android-facebook-intent – Derzu Nov 28 '12 at 13:46
  • not full helpful but your code give show me the right way,So +1 for this..thank you – Zala Janaksinh Feb 07 '14 at 10:15
  • This is what I want. Thank you! – JordanChina Sep 16 '15 at 06:41
  • Hello, @Derzu, In above method there are multiple applications are displayed. I want to get which application used for sharing because i have to call one api after sharing. Is there any way to get which intent is used? Thanks – patel135 Jun 06 '16 at 05:56
  • @Derzu If no application found then what? – Mihir Trivedi Jun 27 '20 at 18:48
7

No you can't. Intent's are supposed to work this way. If you have to force a particular app to open, use explicit intents if the target apps support those. Without knowing the package names or the component names of the target apps, or the type or mime type of data, you can't force a particular app to work on generalized intents.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • 2
    you mean that for facebook for example i must use the Facebook sdk and create facebook application and link my android app to it so as to make wall posts ? – Zak Jul 26 '11 at 09:01
  • Yes, exactly. I don't know if the official facebook app exposes any kind of readymade intents for your use. One of the twitter client has that, but I don't remember which one is that right now. I guess, (Twidroid). – Kumar Bibek Jul 26 '11 at 09:03
  • what if i don't use the installed application .. can i open the browser on the FB page and post the link directly ? – Zak Jul 26 '11 at 09:06
  • That you will have to check if it works on not. I am not sure. – Kumar Bibek Jul 26 '11 at 09:07
  • But, I thought it is possible from my observation? http://stackoverflow.com/questions/19478888/how-to-customize-share-intent-in-android-which-is-possible (I'm not sure how to do that though) – Cheok Yan Cheng Oct 20 '13 at 15:00
  • facebook share dialog can be an option for you. – Rajiv yadav Jan 06 '14 at 05:39
4

First, list all apps to share.

private java.util.List<ResolveInfo> showAllShareApp() {
    java.util.List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
    Intent intent = new Intent(Intent.ACTION_SEND, null);
    intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    intent.setType("text/plain");
    PackageManager pManager = getPackageManager();
    mApps = pManager.queryIntentActivities(intent,
            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
    return mApps;
}

Then, select one

    private void share(ResolveInfo appInfo) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        if (appInfo != null) {
            sendIntent
                    .setComponent(new ComponentName(
                            appInfo.activityInfo.packageName,
                            appInfo.activityInfo.name));
        }
        sendIntent.setType("text/plain");
//        startActivity(Intent.createChooser(sendIntent, "Share"));
        startActivity(sendIntent);
    }

Here's an sample project.Hope it helps.

BruceDu
  • 280
  • 3
  • 8
1

You can get all email client using ACTION_SENDTO like

        Intent getMailClients = new Intent(Intent.ACTION_SENDTO);
        getMailClients.setData(Uri.parse("mailto:"));
        final PackageManager pm = this.getPackageManager();
        final List<ResolveInfo> emailsClients = pm.queryIntentActivities(getMailClients, 0);

        if (emailsClients.size() == 0) {
            Toast.makeText(this, "There are no email clients installed", Toast.LENGTH_LONG).show();
            return;
        }

and then create your own chooser(dialog with list of founded apps). When user click on item you can do smth like this.

        Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
        sendMailIntent.setType(some type like text/plain or other you need);
        ...
        there you can set SUBJECT,EMAILTO, attach files
        ...
        final List<ResolveInfo> matches = pm.queryIntentActivities(sendMailIntent, 0);
        ResolveInfo sendingProgramm = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.equals(clickedResolveInfo.activityInfo.packageName)) {
                sendingProgramm = info;
                sendMailIntent
                        .setClassName(sendingProgramm.activityInfo.packageName, sendingProgramm.activityInfo.name);
                break;
            }
        }
        startActivity(sendMailIntent);

Maybe it will help you.

Raman Branavitski
  • 864
  • 1
  • 9
  • 22