Android-从WebView保存数据 [英] Android - Save data from WebView

查看:284
本文介绍了Android-从WebView保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在应用程序中使用WebView加载网页.我已经在WebView中加载了以下网页URL:

Currently I am using WebView in my application to load Webpage. I have loaded following webpage URL in WebView:

http://domain_name/app/

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

If User is not logged in then URL is redirected to another URL which is login URL. here is login URL:

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

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

After User enter username and password it is redirected to following URL:

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

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

When User successfully logged in then homepage is shown to user.

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

My Problem is that when application is removed from Recent list login data is lost and application is redirected to Login screen.

我有一种解决此问题的方法.首先是在用户成功登录后最初给Login页面URL,我将URL更改为Homepage URL,而不是Login page URL.

I have one way to solve this issue. First is to give Login page URL initially after successful user login I change URL to Homepage URL instead of Login page URL.

还有其他方法可以解决此问题,也可以将登录数据保存到应用程序存储中吗?

Is there any other way to solve this issue or any way to save login data into application storage?

推荐答案

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

I am answering my own question I thought it might be helpful to others.

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

Finally I have solve my problem through Caching in Internal and External Storage(Save data to External Storage if available)

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

First I have created custom Application class which creates cache storage path in internal or external storage.

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;

@Override
public 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;
            }
        }
    }
}

@Override
public File getCacheDir() {
    if (extStorageAppCachePath != null) {
        return extStorageAppCachePath;
    }
    else {
        return super.getCacheDir();
    }
}

}

在Android Manifest文件中,我在application标签下赋予了以下权限和应用程序名称.

In Android Manifest file I have given following permission and Application name under application tag.

<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>

然后,如果有可用的缓存,则从存储中加载最终启用缓存.

Then final enable cache to load from storage if cache is available.

mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

这篇关于Android-从WebView保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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