Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...