My Android HTML application is losing values stored in localStorage when it shuts down. Anyone else see this issue?

后端 未结 5 1106
不知归路
不知归路 2020-12-24 08:01

I have a native Android 2.1 application that hosts a web view. I load up a site that contains javascript that uses the LocalStorage feature. When the application is runnin

相关标签:
5条回答
  • 2020-12-24 08:09
    // Confimed on android 2.1 emulator
    
    // enable javascript localStorage
    
    
    WebSettings webSettings = myWebView.getSettings();
    
    webSettings.setDomStorageEnabled(true);   // localStorage
    
    // eg if your package
    
    // package www.myapp.whatever;
    
    // eg webSettings.setDatabasePath("/data/data/www.myapp.whatever/databases/");
    
    webSettings.setDatabasePath("/data/data/packagename/databases/");
    

    this works

    0 讨论(0)
  • 2020-12-24 08:10

    Just for complete the previous responses which did not allow to resolve the issue in my case.

    I'm working with Android 4.1.1. My app is using local storage within a Webview and I was getting the same issue as in the original question: the local storage works fine until I kill the app. In this case, the data was lost.

    By inspiring me from previous responses, and especially from @diyism, I was able to solve my issue with this:

    String databasePath = this.getApplicationContext().getDir("databases", Context.MODE_PRIVATE).getPath();
    settingsMenu.setDatabasePath(databasePath);
    

    In fact, as written in the setDatabasePath() documentation: to function correctly, this method must be called with a path to which the application can write.

    0 讨论(0)
  • 2020-12-24 08:19

    only need these two lines:

    this.getSettings().setDomStorageEnabled(true); //enable to use "window.localStorage['my']='hello1'", in webview js on >= android 2.0
    this.getSettings().setDatabasePath("/data/data/"+this.context.getPackageName()+"/databases/"); //if no set or wrong path, variables disappear on killed
    
    0 讨论(0)
  • 2020-12-24 08:21

    setDatabasePath() method was deprecated in API level 19. I advise you to use storage locale like this:

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
    }
    
    0 讨论(0)
  • 2020-12-24 08:26

    To make the local storage work for your own WebView in Android, you need to make sure the WebView is using the correct file, and the local storage is enabled like this:

    String packageName = "com.dongshengcn.android";
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath("/data/data/"+packageName+"/databases");
    settings.setDomStorageEnabled(true);
    

    Where "com.dongshengcn.android" should be replaced with your own android app pacakge name.

    0 讨论(0)
提交回复
热议问题