简体   繁体   English

libGDX:共享按钮,用于在例如 Facebook、电子邮件、WhatsApp、Twitter 中共享一个简单的字符串

[英]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.我在 Android Studio 中使用 libGDX。

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将上述接口实现到AndroidLauncher类并编写实现,将实现类的对象共享到核心模块并从核心模块调用共享方法

    @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. PLAYSTORE_LINK是我的AppInfo类中的静态常量 String,您可以保留在自己的类中,也可以替换为 String 值。

     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.您有两个接口,并且在您的ApplicationListener实现的类中使用了两个接口引用。

    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.创建两个接口的父接口并使用父接口实现AndroidLauncher类,并在ApplicationListener实现的类中捕获引用,Downcast 并调用相应的方法。

    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);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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