0

我正在尝试开发一种从 JSInterface 触发应用内购买的方法。

我有两节课。

第一个是 MainActivity,我在其中使用 Iabhelper 实现了 inAppBilling。此活动包含我用来购买物品的方法:

protected void purchaseItem(String sku_item) {
    final String sku = sku_item;
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase){
            if (result.isFailure()) {
                Log.e("PURCHASE","Error : "+ sku);
                return;
            }      
            else if (purchase.getSku().equals(sku)) {
                Log.e("PURCHASE","OK : " + sku);
            }               
        }
    };
    mHelper.launchPurchaseFlow(this, sku, 10001, mPurchaseFinishedListener, "mypurchasetoken");
}

第二个是 JsInterface,它从用户在 webview 中单击获取项目 sku:

public class jsinterface {
Context mContext;

/** Instantiate the interface and set the context */
jsinterface(Context c) { mContext = c; }    

@JavascriptInterface
public void purchaseSku(String sku) {        
    Log.e("JSInterface","Purchase SKU : " + sku);        
    MainActivity cls2= new MainActivity();
    cls2.purchaseItem(sku);
}
}

我的测试表明:

purchaseItem() from MainActivity is ok and functional
purchaseSku() from JsInterface is catching the right sku from user's click
But purchaseSku() seems to fail calling purchaseItem() from MainActivity... Nothing happens. Do you have any idea of what i'm doing wrong and how i can correctly fire purchaseItem() from purchaseSku() ?
4

0 回答 0