当我从Play商店更新应用程序时,如何清除localStorage?

4
我制作了一个使用Angular和Ionic的Android移动应用程序。我已经将其上传到Playstore,并且运行版本为1.0。现在我要在Playstore上更新我的应用程序。
当用户更新应用程序时,本地存储应该被清除。有人能帮我吗?
我想在每次安装我的应用程序时清除本地存储。

在这里查看答案:https://dev59.com/llnUa4cB1Zd3GeqPedh0 - dev.bmax
2个回答

5
  1. 在你的应用中添加版本代码变量。
  2. 每次应用启动时,请检查本地存储是否保存了相同版本代码。如果匹配,则不执行任何操作,否则进行清理并将新代码保存到本地存储。

编辑: 在您的应用程序开头的某个位置:

 /*your currnt app version, 
  you should manually update it here*/
  var version = "200";
  /*get version code stored in localstorage*/
  var savedAppVersion = localStorage.getItem("version");
  /*if there is one check if it is the same*/
  if (savedAppVersion && savedAppVersion === version) {
    // do nothing
  } else {
    /*clear all local storage*/
    localStorage.clear();
    /*save new code to ls*/
    localStorage.setItem("version", version);
  }

编辑2:

你可以使用cordova插件自动获取应用程序版本:

ngCordova

因此,您可以使用$cordovaAppVersion服务:

$cordovaAppVersion.getVersionNumber().then(function (version) {
        // use version here
      });
  }, false);

我该怎么做?别误会,我是新手。请给我提供示例代码。 - user5861300
我怎样可以从Play商店获取应用版本信息? - user5861300
我在设置中存储了一个标志,用于指示我们是否要进行清理。 - cmnardi

0

来自Android文档的快速参考。

public class FeedReaderDbHelper extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";

    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

通过将DATABASE_VERSION增加1,例如DATABASE_VERSION = 2,将调用onUpgrade()方法,然后调用SQL查询以删除表,接着创建新表。
关于Ionic的参考:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])
//inject $crodovaSQLite
.run(function($ionicPlatform, $cordovaSQLite, $rootScope) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleLightContent();
}

  $rootScope.db = $cordovaSQLite.openDB("my.db");
  $cordovaSQLite.execute(db, "DROP TABLE IF EXISTS joke");
  $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS joke(id integer primary key, joke text)");

});
})

这个想法类似于本地实现(删除并重新创建表)。

如果您将此应用提交到Play商店,所有接收更新的用户也将立即获得清理。


我正在使用 Ionic Angular。 - user5861300

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接