How to customize share intent in Android?

半城伤御伤魂 提交于 2019-12-17 02:35:21

问题


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 ?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!