【问题标题】:Implementing In-App Billing with libgdx使用 libgdx 实现应用内计费
【发布时间】:2015-03-01 04:00:31
【问题描述】:

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

}

}

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

【问题讨论】:

    标签: java android libgdx billing


    【解决方案1】:

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

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

    【讨论】:

    • 我不是 100% 确定你的意思。我阅读了这个例子,我的理解是我需要在我的核心项目中有一个接口,其中 Stone 类是,并且该接口需要扩展我在 Android 项目中创建的接口?那么两个项目之间的接口通信呢?因为我需要 Stone 类中的 buttonlistener 来执行购买。
    • 是的,创建具有您的 Stone 类将执行的方法的接口(例如“PurchaseAPI”)。将接口放入您的核心项目中。在您的后端添加一个实现 PurchaseAPI 的类(例如,“公共类 AndroidPurchase 实现 PurchaseAPI”),它实际上会通过 Google API 进行采购。
    • user3602369 能否请您发布 iap 的工作版本,因为所有教程看起来都很复杂?
    猜你喜欢
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多