Android应用内结算 - 购物车中的多个SKU

时间:2014-10-07 15:51:30

标签: android in-app-billing

我正在为我的音乐学校老师开发一个Android应用程序,他希望以钢琴演奏者的形式出售他的PDF格式的印度电影音乐。我已经设计了列表中的每个项目的歌曲列表视图,为用户提供了一个选项,供用户在购物车中添加/删除音乐表。

我创建了我的谷歌控制台开发者帐户,并附加了一个商家帐户。我已经为我的应用程序中提供的每个音乐表创建了托管应用内商品。

每个列表项的数据都是从下面的Song类

创建的
public class Song {
    private String songID;
    private int filmImageID;
    private String songName;
    private String movieName;
    private String composerName;
    private String yearReleased;
    private int songPrice;  
    private int mp3File;    
    private Boolean isPlaying;
    private Boolean inCart; 

    public Song(String songID, int filmImageID, String songName, String movieName, String composerName,
            String yearReleased, int songPrice, int mp3File, Boolean isPlaying, Boolean inCart) {
        super();
        this.songID = songID;
        this.filmImageID = filmImageID;
        this.movieName = movieName;
        this.songName = songName;
        this.composerName = composerName;
        this.yearReleased = yearReleased;
        this.songPrice = songPrice;
        this.mp3File = mp3File;     
        this.isPlaying=isPlaying;
        this.inCart=inCart;
    }

我创建了一个类型为Song类的ArrayList,并添加了对象,如下所述

private ArrayList<Song> songsList = new ArrayList<Song>();
songsList.add(new Song("song1_v1",R.drawable.msv_periya_idhuthu_pen,"Andru Vandhadhum Adhey Nila","Periya Idhuthu Pen","M.S. Viswanathan","1963",250,R.raw.msv_andru_vanthathum_athe_nila_pip_1963_200,false,false));
        songsList.add(new Song("song22_v2",R.drawable.msv_paalum_pazhamum,"Aalaiya Maniyin Oosai","Paalum Pazhamum","M.S. Viswanathan","1961",120,R.raw.msv_aalayamaniyin_oosai_pp_1961_120,false,false));

类Song中的变量songID保存了我在谷歌控制台开发者帐户的应用内商品列表中提到的产品ID。

我已使用MusicSheets.java的onCreate()方法启动了与GooglePlay的连接,如下所示

mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {           
    @Override
    public void onIabSetupFinished(IabResult result) {
            if(!result.isSuccess()){
                Toast.makeText(MusicSheets.this, "Fail", Toast.LENGTH_SHORT).show();
            }

            Toast.makeText(MusicSheets.this, "Pass", Toast.LENGTH_SHORT).show();
        }
    });

在用户选择所需的音乐表后,有一个名为imgPayNow的ImageView可以执行购买。 imgPayNow的代码如下所示

imgPayNow = (ImageView) findViewById(R.id.imgPayNow);
imgPayNow.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        if(cartList.size()<1){
            Toast.makeText(MusicSheets.this, "Your cart is empty", Toast.LENGTH_SHORT).show();
            return;
        }

        IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {               

            @Override
            public void onIabPurchaseFinished(IabResult result, Purchase info) {
                if (result.isFailure()) {
                    Toast.makeText(MusicSheets.this, "Fail", Toast.LENGTH_SHORT).show();
                    return;
                }   
            }
        };

        for(int i=0;i<cartList.size();i++){
            if (mHelper != null) mHelper.flagEndAsync();
            mHelper.launchPurchaseFlow(MusicSheets.this, cartList.get(i), 10001, mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
        }
    }
});

现在我面对上述编码的问题是,当我使用For循环时,会调用launchPurchaseFlow方法来添加到购物车的项目数。我想不出如何在launchPurchaseFlow方法中处理多个SKU的逻辑。

很抱歉被问到这个愚蠢的问题。我是一个业余Android开发人员。请帮我解决这个问题。

谢谢, 作者Srini

0 个答案:

没有答案