简体   繁体   中英

libGDX: Share-button to share a simple String in e.g. Facebook, E-Mail, WhatsApp, Twitter

I have a little problem in my application, I hope somebody can help me. I want to share a simple string from my game on eg social netwoks by tapping on a button. I use libGDX with Android Studio.

Thanks for helpful answers! :)

Use interfacing for your requirement

  1. Create Interface in core module

    public interface Services { void share(); }
  2. Implement above interface to AndroidLauncher class and write implementation, share object of implemented class to core module and from core module call share method

    @Override public void share() { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.app_name); String sAux = "\\nLet me recommend you this game\\n\\n"; sAux = sAux + AppInfo.PLAYSTORE_LINK+getPackageName()+" \\n\\n"; sharingIntent.putExtra(Intent.EXTRA_TEXT, sAux); startActivity(Intent.createChooser(sharingIntent, "Share via")); }

EDIT

  • PLAYSTORE_LINK is a static constant String inside my AppInfo class, You can keep in your own class or you can replace with String value.

     public static final String PLAYSTORE_LINK= "https://play.google.com/store/apps/details?id=";
  • You've two interface and you're using both inferface reference in Your ApplicationListener implemented class.

    1. Either create a parent interface of both interface and implement AndroidLauncher class with parent interface and catch reference in ApplicationListener implemented class, Downcast and call respective method.

    2. Pass two argument in Constructor and keep reference in core module, then call method.

  • Keep reference

    public class GdxTest extends ApplicationAdapter{ Service service; public GdxTest(Service service) { this.service=service; } }
  • Add Listener to your object

    TextButton x= new TextButton("SHARE",skin); x.addListener(new ClickListener(){ @Override public void clicked(InputEvent event, float x, float y) { service.share(); super.clicked(event, x, y); } });
 btn_share.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {

           Gdx.net.openURI("http://share_adress.html");

            super.clicked(event, x, y);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM