Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize share intent in Android?

Tags:

android

share

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 ?

like image 286
Zak Avatar asked Jul 26 '11 08:07

Zak


People also ask

What is Share intent Android?

Sharing Data Using Intents You can let the user share data to any service they want, without writing the sharing code yourself, simply by creating a share intent. Intent intent=new Intent(android. content. Intent. ACTION_SEND); intent.

What is the use of intent createChooser () method?

The system will always present the chooser dialog even if the user has chosen a default one. If your intent created by Intent. createChooser doesn't match any activity, the system will still present a dialog with the specified title and an error message No application can perform this action .

What are the two types of intent in Android?

There are two intents available in android as Implicit Intents and Explicit Intents.


4 Answers

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

like image 173
Derzu Avatar answered Oct 24 '22 08:10

Derzu


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.

like image 29
Kumar Bibek Avatar answered Oct 24 '22 08:10

Kumar Bibek


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.

like image 22
BruceDu Avatar answered Oct 24 '22 09:10

BruceDu


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.

like image 20
Raman Branavitski Avatar answered Oct 24 '22 07:10

Raman Branavitski