public interface AdsControllerBase { public void createView( RelativeLayout layout); public void show(boolean show); public void onStart(); public void onDestroy(); public void onResume(); public void onStop(); }
public class AdMobController implements AdsControllerBase, AdListener { private static final String ADMOB_ID = "___AdMob"; private static final int REQUEST_TIMEOUT = 30000; private AdView adView; private Context c; private long last; public AdMobController(Context activity, RelativeLayout layout) { this.c = activity; createView(layout); last = System.currentTimeMillis() - REQUEST_TIMEOUT; } public void createView(RelativeLayout layout) { if(PreferencesHelper.isAdsDisabled()) return; adView = new AdView((Activity) c, AdSize.BANNER, ADMOB_ID); RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); adParams.addRule(RelativeLayout.CENTER_HORIZONTAL); adView.setAdListener(this); layout.addView(adView, adParams); adView.loadAd(new AdRequest()); } // , 30 public void show(boolean show) { if(adView == null) return; adView.setVisibility((show) ? View.VISIBLE : View.GONE); if (show && (System.currentTimeMillis() - last > REQUEST_TIMEOUT)) { last = System.currentTimeMillis(); adView.loadAd(new AdRequest()); } } @Override public void onReceiveAd(Ad ad) {} @Override public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error) {} @Override public void onPresentScreen(Ad ad) {} @Override public void onDismissScreen(Ad ad) {} @Override public void onLeaveApplication(Ad ad) {} @Override public void onStart() {} @Override public void onDestroy() {} @Override public void onResume() {} @Override public void onStop() {} }
AdsControllerBase ads = new AdMobController(this, layout);
IabHelper
and initialize. If successful, then try to restore the purchase. // id Google Play static final String SKU_ADS_DISABLE = "com.ads.disable"; IabHelper mHelper; private void billingInit() { mHelper = new IabHelper(this, BASE64_PUBLIC_KEY); // ( false) mHelper.enableDebugLogging(true); // ; // , mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { return; } // mHelper. queryInventoryAsync(mGotInventoryListener); } }); }
mGotInventoryListener
- a purchase recovery listener. // . IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { private static final String TAG = "QueryInventoryFinishedListener"; public void onQueryInventoryFinished(IabResult result, Inventory inventory) { LOG.d(TAG, "Query inventory finished."); if (result.isFailure()) { LOG.d(TAG, "Failed to query inventory: " + result); return; } LOG.d(TAG, "Query inventory was successful."); /* * . * , , , ! * . verifyDeveloperPayload(). */ Purchase purchase = inventory.getPurchase(SKU_ADS_DISABLE); PreferencesHelper.savePurchase( context, PreferencesHelper.Purchase.DISABLE_ADS, purchase != null && verifyDeveloperPayload(purchase)); ads.show(!PreferencesHelper.isAdsDisabled()); } };
private void buy(){ if(!PreferencesHelper.isAdsDisabled()){ /* payload . . * . */ String payload = ""; mHelper.launchPurchaseFlow(this, SKU_ADS_DISABLE, RC_REQUEST, mPurchaseFinishedListener, payload); } }
mPurchaseFinishedListener
- listener: // IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if (result.isFailure()) { return; } if (!verifyDeveloperPayload(purchase)) { return; } if (purchase.getSku().equals(SKU_ADS_DISABLE)) { Toast.makeText(getApplicationContext(), "Purchase for disabling ads done.", Toast.LENGTH_SHORT); // , PreferencesHelper.savePurchase( context, PreferencesHelper.Purchase.DISABLE_ADS, true); // ads.show(!PreferencesHelper.isAdsDisabled()); } } };
boolean verifyDeveloperPayload(Purchase p) { String payload = p.getDeveloperPayload(); /* * TODO: * . */ return true; }
PreferencesHelper.loadSettings(this);
Source: https://habr.com/ru/post/203368/
All Articles