Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top