Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android + HTML5(LocalStorage) + Admob: Bug?

I am developing an Phonegap app(Android), which uses javascript/HTML5 localStorage. The app works fine, however when I add the Admob to the app, the localStorage not work. Meaning the stored values are delete when the app is force closed or the phone is restarted.

public class TestActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        // ADMOB: If comment, work.
        /*
        LinearLayout layout = super.root;
        AdView adView = new AdView(this, AdSize.BANNER, **MY_CODE_ADMOB**);
        layout.addView(adView);
        AdRequest request = new AdRequest();
        adView.loadAd(request);
        */
    }
}        

Thanks!!

like image 600
user1147582 Avatar asked Nov 28 '25 17:11

user1147582


1 Answers

You have to delay the code that launches the ad by a few seconds...below worked for me.

public class youActivity extends DroidGap {
private Handler mHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
    mHandler.postDelayed(new Runnable() {
        public void run() {
            doStuff();
        }
    }, 5000); 
}
private void doStuff() {
    final String MY_AD_UNIT_ID = "yourAD_UNIT_ID";
    AdView adView; 
    // Create the adView 
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); 
    LinearLayout layout = super.root; // this is the only change from the sample 
    // Add the adView to it 
    layout.addView(adView); 
    // Initiate a generic request to load it with an ad 
    adView.loadAd(new AdRequest());
}
}
like image 174
user1184211 Avatar answered Nov 30 '25 05:11

user1184211



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!