简体   繁体   中英

Android in-app billing button

I have a button in an activity, i would like when a user click a button it makes him pay a fee to access it. with my code the button isn't clickable at all, when I take off this line " upper.setEnabled(true); " the button is clickable but the billing isnt starting.

this is my code :

public class Main extends Activity {
private Vibrator myVib;
static final String ITEM_SKU = "android.test.purchased";
IabHelper mHelper;
private static final String TAG = "com.dave.where.inappbilling";
private Button upper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

    String base64EncodedPublicKey = "<My License key>";

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed: " + result);
            } else {
                Log.d(TAG, "In-app Billing is set up OK");
            }
        }
    });

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,
                Purchase purchase) {
            if (result.isFailure()) {
                // Handle error
                return;
            } else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                upper.setEnabled(false);
            }

        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {

            if (result.isSuccess()) {
                upper.setEnabled(true);
            } else {
                // handle error
            }
        }
    };


    upper = (Button) findViewById(R.id.upper);

     upper.setEnabled(false);


    upper.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getApplicationContext(),
                    Exerciseupper.class);
            startActivity(intent);

        }
    });


}

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
        } else {
            // mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
            // mConsumeFinishedListener);
        }
    }
};

public void upperClicked(View view) {

    upper.setEnabled(true);
}

public void buyClick(View view) {
    // mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
    // mPurchaseFinishedListener, "mypurchasetoken");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null)
        mHelper.dispose();
    mHelper = null;
}
}
upper.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getApplicationContext(),
                Exerciseupper.class);
        startActivity(intent);

    }
});

change this to:

upper.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getApplicationContext(),
                Exerciseupper.class);
        startActivity(intent);

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM