Intro
Recently I worked on a freelancing project for Android. It was a port of an app/game from iOS to Android. The app included sharing with Facebook, Twitter, SMS and email. This the story about and android developer trying to do a simple thing (share on Facebook) the hard way.The problem
Sharing on Android is simple, it's 5 lines of code:But that's just in theory, in practice it's a little different. The problem: Facebook.
Facebook shows in the list for the ACTION_SEND intent but it ignores the text sent as EXTRA_TEXT.
StackOverflow is full with topic about the broken Facebook share on Android:
There is even a bug reported on Facebook developers but the status of the problem is "By Design". They are not fixing it any time soon.
After a round of swearing at Zuck I started looking for a solution on Google and I found a blog post with an elegant solution (it's actually a hack).
The idea is simple:
- Create your intent
- Query the Android PackageManager for apps that support the intent
- Manually create the Share dialog using AlertDialog
- When the user selects an app
- If the app is Facebook - Share using the Facebook SDK
- If it's any other app let Android handle the sharing
Integrating the FacebookSDK for Android
I started with visiting the developers.facebook.com website, I registered as a developer, downloaded the FacebookSDK and started reading the Getting Started guide.
Basically you have to download the SDK, import it into Eclipse, and reference it as a library project for your app. Then you have to create a Facebook app in your Facebook developer console, and add a Native Android application to your Facebook app. It's simple you just enter your package name, the full activity name (where you do your Facebook authentication) and a keyhash. You can enter more than one keyhash (ex. one with your debug key and one with your release key). There is a nice command in the guide for getting the key, it's a simple command you write in the terminal.
Don't do that. I tried with two keys (debug and release) and it gave me a wrong key hash twice. Use the second method (under troubleshooting). Alter the onCreate method and add:
It's the safer option.
To login with Facebook you just need to call one method, implement the callback and add on line of code to the onActivityResult method on your activity.
To login with Facebook you just need to call one method, implement the callback and add on line of code to the onActivityResult method on your activity.
Sharing a status update What I tried first
First I tried using the Request class and the newStatusUpdateRequest method. It didn't work.
What worked
To post a status update first you need the publish_stream permission. You can find the code for asking the user for permission and publishing to the feed here.
Finishing toughts
I'm not sure why Facebook chose to handle the share intent but ignore the text. The should either do things right - handle the intent and text or not show in the list when we chose ACTION_SEND.
Another question is why are they a TOP DEVELOPER on the Play Store when the can't (or won't) handle a simple ACTION_SEND intent.