[英]Implementing In-App Billing with libgdx
所以這個問題我已經解決了大約2個月,我真的需要一些幫助。 我正在做一個應用程序,我希望用戶能夠購買某些東西。
我已經盡力遵循了本指南: http : //developer.android.com/google/play/billing/billing_integrate.html
問題是,我使用Libgdx,這對我來說有點棘手。 我在AndroidLauncher類中實現了大部分東西,這是我懷疑的主要東西。 看起來像這樣:
public class AndroidLauncher extends AndroidApplication {
IInAppBillingService mservice;
ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
mservice = IInAppBillingService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mservice = null;
}
};
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new TombStone(null), config);
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
// The list the holds all the items available for purchase
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add("premiumUpgrade");
skuList.add("gas");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
Bundle skuDetails = mservice.getSkuDetails(3, getPackageName(), "inapp", querySkus);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mservice != null) {
unbindService(connection);
}
}
}
盡管該類別不是實際“商店”屏幕所在的類別。 這是此類:
public class Stones implements Screen {
OrthographicCamera camera;
final TombStone game;
private Stage stage;
private TextureAtlas atlas;
private Skin skin;
private Table table;
private BitmapFont font;
ImageButton btnArrow, btnArrowLeft, imageButton1, imageButton2, imageButton3, imageButton4, imageButton5, imageButton6;
public Texture background, testImage, arrow, arrowLeft;
public TextureAtlas buttonAtlas;
public Stones(TombStone gam) {
game = gam;
game.assets.load();
loadStore();
camera = new OrthographicCamera();
camera.setToOrtho(false, 136, 204);
}
public void loadStore() {
background = game.assets.storeBackground;
testImage = game.assets.image;
arrow = game.assets.arrow;
arrowLeft = game.assets.arrowLeft;
}
@Override
public void render(float delta) {
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.draw(background, 0, 0, 136, 204);
game.font.setScale(0.5f);
game.font.draw(game.batch, "This is the current selection of tombstones! Simply click the image of the stone you want to buy it.", 50, 200);
game.batch.end();
//Draws the ImageButtons
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
Skin skin = new Skin();
//ArrowButtons
Skin skinTwo = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("arrow.pack"));
skinTwo.addRegions(buttonAtlas);
ImageButtonStyle styleTwo = new ImageButtonStyle();
TextureRegionDrawable arrowImage = new TextureRegionDrawable(new TextureRegion(new Texture("arrowLeft.png")));
styleTwo.up = skinTwo.newDrawable(skinTwo.newDrawable(arrowImage));
styleTwo.down = skinTwo.newDrawable(skinTwo.newDrawable(arrowImage));
//ImageButtons - Category buttons
ImageButtonStyle style = new ImageButtonStyle();
TextureRegionDrawable ibimage = new TextureRegionDrawable(new TextureRegion(new Texture("image.png")));
style.up = skin.newDrawable(skin.newDrawable(ibimage));
style.down = skin.newDrawable(skin.newDrawable(ibimage));
imageButton1 = new ImageButton(style);
imageButton2 = new ImageButton(style);
imageButton3 = new ImageButton(style);
imageButton4 = new ImageButton(style);
imageButton5 = new ImageButton(style);
imageButton6 = new ImageButton(style);
//ArrowButtons implement
btnArrow = new ImageButton(styleTwo);
btnArrow.setSize(150, 150);
btnArrow.setPosition(450, 10);
/*table = new Table();
table.setBounds(100, 100, 100, 100);
skin = new Skin();
font = new BitmapFont();
TextureRegion image = new TextureRegion(testImage);
stage = new Stage();
Gdx.input.setInputProcessor(stage);
//ImageButtonStyle style = new ImageButtonStyle(skin.get(ButtonStyle.class));
//style.imageUp = new TextureRegionDrawable(image);
//style.imageDown = new TextureRegionDrawable(image);
ImageButton iconButton = new ImageButton(skin);
//Button imgButton = new Button(new Image(image), skin);*/
//Stage for the BuyImages
stage = new Stage();
table = new Table();
table.setBounds(20, 320, 175, 1050);
table.add(imageButton1).pad(5).size(175, 175).row();
table.add(imageButton2).pad(5).size(175, 175).row();
table.add(imageButton3).pad(5).size(175, 175).row();
table.add(imageButton4).pad(5).size(175, 175).row();
table.add(imageButton5).pad(5).size(175, 175).row();
table.add(imageButton6).pad(5).size(175, 175).row();
stage.addActor(table);
stage.addActor(btnArrow);
Gdx.input.setInputProcessor(stage);
//Backbutton
btnArrow.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new StoreScreen(game));
}
});
imageButton1.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
}
});
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
因此,我懷疑是否需要以某種方式將第一堂課與第二堂課聯系起來,才能使用我在第一堂課中實現的內容? 但是如何? 在那之后,我有點迷失了。 我看過很多教程,但都沒有更具體地描述我的問題。 幫助將是巨大的! 提前致謝
有一個Libgdx擴展庫,用於以跨平台方式處理應用程序內購買: https : //github.com/libgdx/gdx-pay 。 它尚未像其他libgdx一樣經過良好的測試,但是它可能是一個不錯的起點。 使用此方法可以避免您的問題。
但是,獲得希望的工作並不難(希望如此),因此這也是一種可行的方法。 基本上,您需要獲取與平台無關的應用程序代碼,以與特定於后端的代碼進行對話。 通常,這是在Libgdx應用程序中通過定義平台無關代碼(您的“ Stones”類)可以使用的接口,並在每個受支持的后端中定義該接口的實現來完成的。 因此,在您的情況下,您可以將該接口添加到您的AndroidLauncher中(但是創建一個實現它的新類可能更好)。 如果您有任何其他后端(例如,桌面后端),則需要創建一個存根API的實現,以便您的代碼仍可以在桌面(或任何一個)上運行。 然后,當每個后端啟動時,它必須將其接口的實現傳遞給平台無關類的構造函數。 Libgdx文檔在此處對此進行了概括介紹(它們使用排行榜的示例): https : //github.com/libgdx/libgdx/wiki/Interface-with-platform-specific-code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.