[英]libGDX: Share-button to share a simple String in e.g. Facebook, E-Mail, WhatsApp, Twitter
我的申請有一個小問題,我希望有人可以幫助我。 我想通過點擊按鈕在社交網絡上分享我的游戲中的一個簡單字符串。 我在 Android Studio 中使用 libGDX。
感謝您提供有用的答案! :)
根據您的要求使用接口
在核心模塊中創建接口
public interface Services { void share(); }
將上述接口實現到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")); }
編輯
PLAYSTORE_LINK
是我的AppInfo
類中的靜態常量 String,您可以保留在自己的類中,也可以替換為 String 值。
public static final String PLAYSTORE_LINK= "https://play.google.com/store/apps/details?id=";
您有兩個接口,並且在您的ApplicationListener
實現的類中使用了兩個接口引用。
創建兩個接口的父接口並使用父接口實現AndroidLauncher
類,並在ApplicationListener
實現的類中捕獲引用,Downcast 並調用相應的方法。
在構造函數中傳遞兩個參數並在核心模塊中保留引用,然后調用方法。
保留參考
public class GdxTest extends ApplicationAdapter{ Service service; public GdxTest(Service service) { this.service=service; } }
將偵聽器添加到您的對象
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.