Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
615 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...