2

I would share a link on facebook through a button on libgdx (for Android).

This code works but just the web browser is launched :

Gdx.net.openURI("https://www.facebook.com/sharer/sharer.php?u=http://www.example.com");

I want to open this sharing view with the Facebook app.

If someone could assist

thanks

Samy Sup
  • 298
  • 2
  • 16
  • Do this: http://stackoverflow.com/a/10213314/506796. If you are also running the desktop version of your game, then you need to create an interface for handling this, and create an implementation of the interface in your Android project and pass it to your game. – Tenfour04 May 15 '14 at 17:33
  • Thank you for your help ! This can help me but how can I launch an intent through libgdx. I saw a tutorial with the interface and its implementation but I didn't understand. – Samy Sup May 16 '14 at 15:06

1 Answers1

2

This is a general method for making calls to Android APIs in libgdx. I don't think the Facebook app actually has a "share" activity you can go to without implementing the Facebook SDK in your app, so I'll just do a simple example with Facebook links.

Create an interface like this:

public interface FacebookLinkHandler{
    public void openFacebookPage(String facebookAppURI, String facebookWebURL);
}

Then in your Game subclass, add one of these interfaces as a constructor parameter:

public class MyGame extends Game{
    //...
    FacebookLinkHandler mFacebookLinkHandler;

    public MyGame(..., FacebookLinkHandler facebookLinkHandler){
        //...
        mFacebookLinkHandler = facebookLinkHandler;
    }

    //And when you want to go to the link:
    mFacebookLinkHandler.openFacebookPage(
        "fb://profile/4", "https://www.facebook.com/zuck");

}

Then in your Desktop Application, you can create a version that uses only the fallback:

public class DesktopFacebookLinkHandler implements FacebookLinkHandler{
    public void openFacebookPage(String facebookAppURI, String facebookWebURL){
        Gdx.net.openURI(facebookWebURL);
    }
}

and pass one into your MyGame constructor in the DesktopStarter class. And then in the Android Project, you do the alternative:

public class AndroidFacebookLinkHandler implements FacebookLinkHandler{
    Activity mActivity;

    public AndroidFacebookLinkHandler(Activity activity){
        mActivity = activity;
    }

    public void openFacebookPage(String facebookAppURI, String facebookWebURL){
        Intent intent = null;        
        try {
            mActivity.getPackageManager().getPackageInfo("com.facebook.katana", 0);
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookAppURI));
        } catch (NameNotFoundException e) {
            intent = new Intent(Intent.ACTION_VIEW, facebookWebURL));
        }
        mActivity.startActivity(intent);
    }
}

and do the same thing. Pass an instance of this into your MyGame class when you instantiate it in your AndroidStarter class.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154