Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android.Share to social networks

I am looking a way, how to share just a text to social networks, using android share intents.Almost everyone offers this code

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, sometext);
startActivity(Intent.createChooser(shareIntent, "Share using"));

but it doesnt work with facebook. Is there any way to share text to all networks. Thanks.

like image 710
Raman Branavitski Avatar asked Jan 21 '14 13:01

Raman Branavitski


People also ask

Can Android apps communicate with each other?

Android inter-process communication At the simplest level, there are two different ways for apps to interact on Android: via intents, passing data from one application to another; and through services, where one application provides functionality for others to use.

What is Getintent?

Getintent is an adtech company developing programmatic solutions. World's best talent in adtech engineering, data science and media strategy brought together to drive development of AI-based programmatic solutions.


2 Answers

You can use social network library to post directly to chosen social networks without any code mess

I use ASNE it's real simply and can not only post but login or getting friends

Edit: as @benka advised I add some code how I made posting to social networks in my app:

Add dependency to chosen social network(facebook and twitter, but ASNE contains more):

dependencies {
...
    compile 'com.github.asne:asne-facebook:0.2.1'
    compile 'com.github.asne:asne-twitter:0.2.1'
...
}

Than simply add mSocialNetworkManager from ASNE

mSocialNetworkManager = (SocialNetworkManager) getFragmentManager().findFragmentByTag(SOCIAL_NETWORK_TAG);
    if (mSocialNetworkManager == null) {
        mSocialNetworkManager = new SocialNetworkManager();
        FacebookSocialNetwork fbNetwork = new FacebookSocialNetwork(this, fbScope);
        mSocialNetworkManager.addSocialNetwork(fbNetwork);
        TwitterSocialNetwork twNetwork = new TwitterSocialNetwork(this, key, secret);
        mSocialNetworkManager.addSocialNetwork(twNetwork);
        getFragmentManager().beginTransaction().add(mSocialNetworkManager, SOCIAL_NETWORK_TAG).commit();
    }

Than show buttons to share my custom buttons in actionbar menu, and at last check login and use

socialNetwork.requestPostMessage("Message");

thats all, I need to get friendslist and I just add

socialNetwork.requestGetFriends(); 
like image 51
Seko Avatar answered Sep 19 '22 22:09

Seko


public void onShareClick(View v) {
    Resources resources = getResources();

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
    emailIntent.setType("message/rfc822");

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);     
    sendIntent.setType("text/plain");


    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if(packageName.contains("android.email")) {
            emailIntent.setPackage(packageName);
        } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            if(packageName.contains("twitter")) {
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
            } else if(packageName.contains("facebook")) {
                // Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
                // One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
                // will show the <meta content ="..."> text from that page with our link in Facebook.
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
            } else if(packageName.contains("mms")) {
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
            } else if(packageName.contains("android.gm")) {
                intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));               
                intent.setType("message/rfc822");
            }

            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);       
}
like image 33
Dakshesh Khatri Avatar answered Sep 17 '22 22:09

Dakshesh Khatri