使用libgdx实现应用内结算

时间:2015-01-02 21:14:01

标签: java android libgdx billing

所以这是我已经处理了大约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

}

}

所以我怀疑我需要将第一类连接到第二类以便能够使用我在第一类中实现的内容?但是怎么样?在那之后,我有点失落。 我看过一堆教程,但没有一个更具体地描述我的问题。 帮助将是巨大的! 提前致谢

1 个答案:

答案 0 :(得分:3)

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

然而,获得你工作的东西并不是太难(希望如此),所以这也是一种可行的方法。基本上,您需要获得与平台无关的应用程序代码以与特定于后端的代码进行通信。通常,这是在Libgdx应用程序中通过定义平台无关代码(您的&#34; Stones&#34;类)可以使用的接口,以及在每个受支持的后端中定义该接口的实现来完成的。因此,在您的情况下,您可以将该接口添加到AndroidLauncher(但最好创建一个实现它的新类)。如果您有任何其他后端(例如,桌面后端),您将需要创建一个存根API的实现,以便您的代码仍然可以在桌面上运行(或以其中任何一个为准)。然后当每个后端启动时,它必须将其接口的实现传递给与平台无关的类的构造函数。 Libgdx文档总体上涵盖了这一点(他们使用排行榜的例子):https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code