[英]Use shared preference to call a method only at the time when application runs first time in Android?
I am working on a wallpaper application in android in which i need to copy few images from assets to SDCARD location for applicaiton. 我正在使用android中的墙纸应用程序,在该应用程序中,我需要将资产中的一些图像复制到SDCARD位置以进行申请。 But this process must to be done on application runs 1st time.
但是必须在第一次运行应用程序时执行此过程。 It must not happen when user use the application in future.
将来用户使用该应用程序时一定不能发生。 I planned to use Shared Preference for this purpose.
我计划为此使用共享首选项。 But not getting success.
但是没有成功。
I have initialized the preferences as private member of class. 我已经将首选项初始化为类的私有成员。
private SharedPreferences preferences=null;
private boolean flagCopy;
//USING Shared Preferences FOR COPY ASSETS------
if(preferences!=null){
flagCopy = preferences.getBoolean("COPY_ASSETS", DO_NOT_COPY_ASSESTS);
}else{
preferences =getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("COPY_ASSETS", COPY_ASSESTS); // value to store
editor.commit();
flagCopy = true;
}
if(flagCopy){//IF FLAG IS FALSE THAN COPY THE IMAGES TO SDCARD FROM RES
CopyAssets();
}
your preferences object is always null, so it will always go to the second part where you are making flagcopy = true. 您的偏好设置对象始终为null,因此它将始终转到您使flagcopy = true的第二部分。 see below code
见下面的代码
preferences =getPreferences(MODE_PRIVATE);
if(preferences!=null){
flagCopy = preferences.getBoolean("COPY_ASSETS", COPY_ASSETS);
if(flagcopy == COPY_ASSETS){
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("COPY_ASSETS", DO_NOT_COPY_ASSESTS);
editor.commit();
flagCopy = true;
}
if(flagCopy){
CopyAssets();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.