通过JsInterface从另一个活动调用方法

时间:2015-06-02 10:10:10

标签: android android-jsinterface

我正试图开发一种方法来解决从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() ?

0 个答案:

没有答案