I’m using AndroidBillingLibrary to implement in-app billing. Purchasing item should disable ads in all activities. But when I tested my app (at first item was “android.test.purchase” and then real item) happened following: after the purchase ads were disabled, but after restarting the application ads are shown up again.
Item is published in developer’s console, when I purchased item with test account Google Play said that all is Ok.
Purchase button is located in the settings of my application. Here is part of the code:
public class PreferencesActivity extends SherlockPreferenceActivity {
public final static String PUBLIC_KEY = "my public key";
public final static String ANDROID_ADS_ITEM = "ads.buy";
private AbstractBillingObserver billingObserver;
...
@Override protected void onDestroy(){
BillingController.unregisterObserver(billingObserver);
super.onDestroy();
}
private void adsDisable(){
final Preference adsDisable = (Preference) findPreference("disableAds");
billingObserver = new AbstractBillingObserver(this){
public void onRequestPurchaseResponse(String itemId, ResponseCode response){
}
public void onPurchaseStateChanged(String itemId, PurchaseState state){
}
public void onBillingChecked(boolean supported){
if(!supported)
adsDisable.setEnabled(false);
else if(!billingObserver.isTransactionsRestored())
BillingController.restoreTransactions(PreferencesActivity.this);
}
public void onSubscriptionChecked(boolean supported) {
// TODO Auto-generated method stub
}
};
BillingController.setConfiguration(new IConfiguration(){
public String getPublicKey(){
return PUBLIC_KEY;
}
public byte[] getObfuscationSalt(){
return new byte[]{ 41, -90, -116, -41, 66, -53, 122, -110, -127, -96, -88, 77, 127, 115, 1, 73, 57, 110, 48, -116 };
}
});
BillingController.registerObserver(billingObserver);
BillingController.checkBillingSupported(this);
adsDisable.setOnPreferenceClickListener(new OnPreferenceClickListener(){
public boolean onPreferenceClick(Preference preference){
BillingController.requestPurchase(PreferencesActivity.this, ANDROID_ADS_ITEM, true, null);
return false;
}
});
boolean purchased = BillingController.isPurchased(this, ANDROID_ADS_ITEM);
if(purchased){
adsDisable.setTitle(R.string.disableAdsYes_prefs); adsDisable.setEnabled(false);
}
}
And this is how I try to disable ads:
public class FirstActivity extends SherlockActivity {
public final static String ANDROID_ADS_ITEM = "ads.buy";
private AdView adView;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
...
String adId = "my ad id";
boolean purchased = BillingController.isPurchased(this, ANDROID_ADS_ITEM);
if(purchased){
layout = (LinearLayout) findViewById(R.id.ads);
layout.removeView(layout);
} else {
adView = new AdView(this, AdSize.BANNER, adId);
layout = (LinearLayout) findViewById(R.id.ads);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}
}
....
}
Please, help. And sorry for my English.
Most likely the problem is that you’re initialising the library configuration (public key and salt) in your preference activity. This means that the library will not be properly configured until you open preferences. Quoting the README:
In this case, when you’re calling
isPurcharsedin your FirstActivity the billing controller doesn’t have a salt to unobfuscate the database.Check out DungeonsRedux for an example of setting the configuration in the Application subclass.