开发者

How to customize share intent in Android?

开发者 https://www.devze.com 2023-03-23 02:29 出处:网络
Now i can use the share intent to open the share dialog Intent intent = new Intent(Intent.ACTION_SEND);

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

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.set开发者_Go百科Type("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 ?


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消