简体   繁体   English

仅在应用程序首次在Android中运行时才使用共享首选项来调用方法吗?

[英]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.

相关问题 检测何时首次运行android应用 - Detect when an android app runs for the first time 静态变量数据在应用程序运行时第一次不反映,并且在Android中从第二次开始不反映 - Static variables data is not reflecting for the first time when the application runs and reflecting from the second time onwards in android 在共享首选项中,当调用 editor.clear() 方法时,它只删除值或删除键值对 - In shared preference, when call editor.clear() method, It delete only values or delete key value pairs Android:如何在服务首次运行时拦截 - Android: how to intercept when a Service runs for the first time 共享首选项同时给出 - Shared Preference giving same time 单击首选项标题Android时调用方法 - Call a method when click on preference header Android 如何管理共享首选项,当共享首选项存储大量数据时,编辑时间太长? - How to manage shared preference, it take too long time to edit when shared preference have store large data? 应用程序启动时的共享首选项 - Shared Preference when Application starts 如何使用共享首选项第一次而不是第二次打开视图寻呼机? - How to open the view pager for first time and not on the second time using shared preference? 在android中第一次启动应用程序时,只显示一次消息对话框 - showing a message dialog only once , when application is launched for the first time in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM