Android– 从WebView保存数据

Android– 从WebView保存数据,第1张

概述目前我在我的应用程序中使用WebView来加载网页.我在WebView中加载了以下网页URL:http://domain_name/app/如果用户未登录,则URL将重定向到另一个URL,即登录URL.这是登录URL:http://domain_name/app/index.php?r=site/login用户输入用户名和密码后,会将其重定向到以下URL:htt

目前我在我的应用程序中使用WebVIEw来加载网页.我在WebVIEw中加载了以下网页URL:

http://domain_name/app/

如果用户未登录,则URL将重定向到另一个URL,即登录URL.这是登录URL:

http://domain_name/app/index.PHP?r=site/login

用户输入用户名和密码后,会将其重定向到以下URL:

http://domain_name/app/index.PHP?r=site/homepage

当用户成功登录后,将向用户显示主页.

我的问题是,当从最近列表中删除应用程序时,登录数据将丢失,应用程序将重定向到“登录”屏幕.

我有一种方法可以解决这个问题.首先是在用户成功登录后首先提供登录页面URL我将URL更改为主页URL而不是登录页面URL.

有没有其他方法可以解决此问题或以任何方式将登录数据保存到应用程序存储中?

解决方法:

我正在回答我自己的问题,我认为这可能对其他人有所帮助.

最后,我通过内部和外部存储中的缓存解决了我的问题(如果可用,将数据保存到外部存储)

首先,我创建了自定义Application类,它在内部或外部存储中创建缓存存储路径.

MyApplication.java

import androID.app.Application;import androID.os.Environment;import java.io.file;public class MyApplication extends Application {protected file extStorageAppBasePath;protected file extStorageAppCachePath;@OverrIDepublic voID onCreate() {    super.onCreate();    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {        file externalStorageDir = Environment.getExternalStorageDirectory();        if (externalStorageDir != null) {            extStorageAppBasePath = new file(externalStorageDir.getabsolutePath() +                    file.separator + "AndroID" + file.separator + "data" +                    file.separator + getPackagename());        }        if (extStorageAppBasePath != null) {            extStorageAppCachePath = new file(extStorageAppBasePath.getabsolutePath() +                    file.separator + "cache");            boolean isCachePathAvailable = true;            if (!extStorageAppCachePath.exists()) {                isCachePathAvailable = extStorageAppCachePath.mkdirs();            }            if (!isCachePathAvailable) {                extStorageAppCachePath = null;            }        }    }}@OverrIDepublic file getCacheDir() {    if (extStorageAppCachePath != null) {        return extStorageAppCachePath;    }    else {        return super.getCacheDir();    }}

}

在AndroID Manifest文件中,我在应用程序标记下给出了以下权限和应用程序名称.

<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE"/> <application        androID:name=".MyApplication"        androID:icon="@drawable/ic_launcher"        androID:label="@string/app_name"        androID:theme="@style/Apptheme" >........................................</application>

然后,如果缓存可用,则最后启用缓存以从存储加载.

mWebVIEw.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
总结

以上是内存溢出为你收集整理的Android – 从WebView保存数据全部内容,希望文章能够帮你解决Android – 从WebView保存数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096417.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存