使用 libgdx 实施应用内结算

Implementing In-App Billing with libgdx

这个问题我已经处理了大约 2 个月了,我真的需要一些帮助。我正在做一个应用程序,我希望用户能够购买某些东西。

我已尽我所能遵循本指南:http://developer.android.com/google/play/billing/billing_integrate.html

问题是,我使用的是 Libgdx,这对我来说有点棘手。我在 AndroidLauncher class 中实现了大部分内容,我怀疑这是主要的。看起来像这样:

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

}

尽管 class 不是实际 "store" 屏幕所在的 class。也就是这个class:

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

}

}

所以我怀疑我需要 link 第一个 class 到第二个才能以某种方式使用我在第一个中实现的东西?但是怎么办?在那之后我有点迷路了。 我看了一堆教程,但没有一个更具体地描述了我的问题。 帮助将是巨大的! 提前致谢

有一个 Libgdx 扩展库用于以跨平台方式处理应用内购买:https://github.com/libgdx/gdx-pay。它没有像 libgdx 的其余部分那样经过充分测试,但它可能是一个很好的起点。使用它可以解决您的问题。

然而,获得你所拥有的东西并不太难(希望如此),所以这也是一种可行的方法。基本上,您需要让独立于平台的应用程序代码与特定于后端的代码对话。通常,这是在 Libgdx 应用程序中通过定义平台独立代码(您的 "Stones" class)可以使用的接口,并在每个支持的后端定义该接口的实现来完成的。因此,在您的情况下,您可以将该接口添加到您的 AndroidLauncher(但最好创建一个新的 class 来实现它)。如果你有任何其他后端(例如,桌面后端),你需要创建一个实现来存根 API 这样你的代码仍然会 运行 在桌面(或任何一个)上。然后当每个后端启动时,它必须将其接口的实现传递给平台无关class的构造函数。 Libgdx 文档在这里概括介绍了这一点(他们使用排行榜的示例):https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code