我只是实现SharedPreferences
。
这是我的Java代码
SharedPreferences sp = getSharedPreferences("adsBannerFull", MainHelloballi.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isItFirstTime", true);
editor.commit();
if (sp.getBoolean("isItFirstTime", false)){
closeButton = (ImageView) findViewById(R.id.close_button);
adsbannerFull = (WebView) findViewById(R.id.adsbanner_full);
adsbannerFull.setVisibility(View.VISIBLE);
adsbannerFull.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
adsbannerFull.getSettings().setLoadsImagesAutomatically(true);
adsbannerFull.getSettings().setDatabaseEnabled(true);
adsbannerFull.getSettings().setAppCacheEnabled(true);
adsbannerFull.getSettings().setDomStorageEnabled(true);
adsbannerFull.getSettings().setJavaScriptEnabled(true);
adsbannerFull.loadUrl("http://api.hellobaliapps.com/ads/main_ads_big");
}
else {
adsbannerFull = (WebView) findViewById(R.id.adsbanner_full);
adsbannerFull.setVisibility(View.GONE);
}
我将此代码放在onCreate
上。
因此,第一次调用它的Activity
将显示adsbannerFull
,但当用户转到Y Activity
时,请返回X Activity
adsbannerFull
不再显示。
我按照本指南http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126&aaid=146进行了改进。但它不起作用,但没有错误。
任何人都可以帮助我?
答案 0 :(得分:0)
SharedPreferences sp = getSharedPreferences("adsBannerFull", MainHelloballi.MODE_PRIVATE);
/* Initially, sp would be null and later on, sp.getBoolean would be false.
If sp is null or sp.getboolean is true, the below statement block would be executed.
*/
if (sp.getBoolean("isItFirstTime", true)){
/*First time only this statement block would be called.
All other times this statement block would never be called.*/
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isItFirstTime", false);
editor.commit();
closeButton = (ImageView) findViewById(R.id.close_button);
adsbannerFull = (WebView) findViewById(R.id.adsbanner_full);
// Remaining code here .........
}// End of if block
/// Remaining code here..........
答案 1 :(得分:0)
尝试这种方式:
声明:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
初始化:
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
放价值:
editor.putBoolean("isItFirstTime", true);
editor.commit();
随时随地获取:
Boolean bool_val=prefs.getBoolean("isItFirstTime",false);
现在你检查你的情况:
if(bool_val)
{
true_Value;
}
else
{
false_Value;
}
如果需要,请使用System.out.println(...);
进行检查