📜 ⬆️ ⬇️

Paid and free applications: two in one



About how difficult to create an application with support for in-app purchase on Habré already told ( here and here ) - too much code for such simple functionality.

Even support for payment via PayPal is much easier to implement .
')
But thanks to Robot Media SL and the open-source Android Billing Library , our life is simplified. Much easier.


Consider an example of how an application may appear in which there is a paid function - Remove Ads .

First of all, the Activity in which the payment function will be used should extend the AbstractBillingActivity . In my case, the button was added to Settings , so I had to slightly correct this class, so that it expands not the Activity , but the PreferenceActivity .

The code itself looks like this:
public class Preferences extends AbstractBillingActivity { public static final String ANDROID_MARKET_ITEM = "my_managed_item_id"; private boolean billingSupported = false; @Override protected void onCreate(Bundle savedInstanceState) { ... Preference buyPref = (Preference) findPreference("pref_billing_buy"); buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { //         public boolean onPreferenceClick(Preference preference) { //       if (checkBillingSupported() != BillingStatus.SUPPORTED) { //  ,     showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); } else { //         Boolean purchased = BillingController.isPurchased(getApplicationContext(), ANDROID_MARKET_ITEM); if (!purchased) { //    (       ?   //    ?),     restoreTransactions(); //   (       ) - //     -  restoreTransactions, //      purchased = BillingController.isPurchased(getApplicationContext(), ANDROID_MARKET_ITEM); if (!purchased) { // ,        requestPurchase(ANDROID_MARKET_ITEM); } } } return true; } }); @Override public void onBillingChecked(boolean supported) { billingSupported = supported; } @Override public void onPurchaseCancelled(String itemId) { Log.i(TAG, "Transaction has been cancelled: "+itemId); } //   ,       UI @Override public void onPurchaseExecuted(String itemId) { Log.i(TAG, "User bought ad-free version: "+itemId); CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy"); buyPref.setEnabled(false); } @Override public void onPurchaseRefunded(String itemId) { Log.i(TAG, "Transaction has been refunded: "+itemId); } //     ,    @Override public byte[] getObfuscationSalt() { return new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; } //      @Override public String getPublicKey() { return "my_key_is_here"; } 


Simply? Very simple.

Well, just in case, I will publish my code in order to correctly remove the advertisement (in my case - AdMob), so that it is not just hidden, but not downloaded:
 Boolean purchased = BillingController.isPurchased(getApplicationContext(), Preferences.ANDROID_MARKET_ITEM); if (purchased) { LinearLayout adContainer = (LinearLayout) findViewById(R.id.bridges_list); View admobAds = (View) findViewById(R.id.adView); adContainer.removeView(admobAds); } 


The author actively develops the library, listens to the wishes, just recently published the source code of the application with an example of using the library.

Source: https://habr.com/ru/post/124098/


All Articles