Android的:如何重置FirstRun共享preferences时,我的应用程序更新? [英] Android: How to reset FirstRun SharedPreferences when my app is updated?

查看:97
本文介绍了Android的:如何重置FirstRun共享preferences时,我的应用程序更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序拷贝从RES /生在第一次运行该文件的SD卡上。我希望它更新所有后续应用程序更新的文件。我怎样才能把它重新设置firstrun preference为true每一个应用程序更新?

下面是相关code:

  / **
     *得到,如果这是第一次运行
     *
     * @返回返回true,如果这是第一次运行
     * /
        公共布尔getFirstRun(){
        回报米prefs.getBoolean(firstRun,真正的);
     }

     / **
     *存储第一次运行
     * /
     公共无效setRunned(){
        共享preferences.Editor编辑= M prefs.edit();
        edit.putBoolean(firstRun,假);
        edit.commit();
     }

     共享preferences米preFS;

     / **
     *设置preferences存储
     * /
     公共无效firstRun preferences(){
        上下文mContext = this.getApplicationContext();
        米preFS = mContext.getShared preferences(对myApp preFS,0); // 0 =模式专用。只有这个程序可以读取这些preferences
     }

     公共无效setStatus(字符串状态文本){
         共享preferences.Editor编辑= M prefs.edit();
         edit.putString(状态,状态文本);
         edit.commit();
      }

}
 

解决方案

在我的应用程序,我保存在我的共享preferences应用程序的版本code。在每次启动时,我检查,看看是否保存的版本code是比我目前的版本code低。如果是,我展现出有什么新的对话框。

给这个codeA抡 - 我在我的主要活动的onCreate使用它:

  PackageInfo PINFO;
    尝试 {
        。PINFO = getPackageManager()getPackageInfo(getPackageName(),PackageManager.GET_META_DATA);
        如果(prefs.getLong(lastRunVersion code,0)< pInfo.version code){
            // TODO:在这里处理你首次运行情况

            编辑EDITOR = prefs.edit();
            editor.putLong(lastRunVersion code,pInfo.version code);
            editor.commit();
        }
    }赶上(的NameNotFoundException E){
        // TODO东西pretty的严重出错了,如果你来到这里......
        e.printStackTrace();
    }
 

preFS是私人共享preferences对象。这工作,如果它是真正的第一次运行,并进行升级。在首次运行code年底,editor.putLong更新您的共享preferences与您的应用程序的当前版本code所以下一次运行不会触发您的首轮$ C $ ℃。

这也有利于从一个事实,即你的版本code必须增加应用程序被视为升级的市场,所以你不必记住改变一个单独的值来检测首次运行

My app copies files over from res/raw to the sdcard on first run. I want it to update those files on every subsequent app update. How can i have it reset the firstrun preference to true on every app update?

Here is the relevant code:

/**
     * get if this is the first run
     *
     * @return returns true, if this is the first run
     */
        public boolean getFirstRun() {
        return mPrefs.getBoolean("firstRun", true);
     }

     /**
     * store the first run
     */
     public void setRunned() {
        SharedPreferences.Editor edit = mPrefs.edit();
        edit.putBoolean("firstRun", false);
        edit.commit();
     }

     SharedPreferences mPrefs;

     /**
     * setting up preferences storage
     */
     public void firstRunPreferences() {
        Context mContext = this.getApplicationContext();
        mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
     }

     public void setStatus(String statustext) {
         SharedPreferences.Editor edit = mPrefs.edit();
         edit.putString("status", statustext);
         edit.commit();
      }

}

解决方案

In my app, I save in my shared preferences the version code of the app. At every startup, I check to see if the saved version code is lower than my current version code. If it is, I show a "what's new" dialog.

Give this code a whirl - I use it in my main activity's onCreate:

    PackageInfo pInfo;
    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
        if ( prefs.getLong( "lastRunVersionCode", 0) < pInfo.versionCode ) {
            // TODO: Handle your first-run situation here

            Editor editor = prefs.edit();
            editor.putLong("lastRunVersionCode", pInfo.versionCode);
            editor.commit();
        }
    } catch (NameNotFoundException e) {
        // TODO Something pretty serious went wrong if you got here...
        e.printStackTrace();
    }

prefs is a private SharedPreferences object. This works if it's truly the first run, and for upgrades. At the end of the first-run code, the editor.putLong updates your shared preferences with the current version code of your app so the next run doesn't trigger your first-run code.

This also benefits from the fact that your version code must increase for the app to be seen as an upgrade by the market, so you don't need to remember to change a separate value to detect the first-run.

这篇关于Android的:如何重置FirstRun共享preferences时,我的应用程序更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆