如何将登录详细信息存储在 Android 的本地存储中
Posted
技术标签:
【中文标题】如何将登录详细信息存储在 Android 的本地存储中【英文标题】:How to store the Login details on Local storage in Android 【发布时间】:2020-03-09 23:29:17 【问题描述】:我正在开发一个基本的 android 应用程序,只有一个登录页面和一个主页,主页上包含 WebView。 我在这里所做的是尝试借助登录页面中的共享首选项保存登录详细信息(用户名和密码),并通过 WebView 的 localStorage 在主页上检索这些信息。
这就是我通过共享首选项在登录页面中保存登录凭据的方式
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
usernameEdit = findViewById(R.id.et_login_email);
passwordEdit = findViewById(R.id.et_login_pass);
private void loginActivity()
String username = usernameEdit.getText().toString().trim();
String password = passwordEdit.getText().toString().trim();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_USERNAME, username);
editor.putString(KEY_PASSWORD, password);
editor.commit();
startActivity(new Intent(LoginActivity.this, HomePage.class));
这是我在 WebView 中使用 LocalStorage 的主页
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
Window window = HomePage.this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
window.setStatusBarColor(ContextCompat.getColor(HomePage.this, R.color.colorPrimary));
swipeRefreshLayout = findViewById(R.id.swipelayout);
myWebView = findViewById(R.id.webview);
WebSettings settings = myWebView.getSettings();
settings.setjavascriptEnabled(true);
settings.setDatabasePath("/data/data/" + this.getPackageName() + "/databases/");
settings.setDomStorageEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient()
@Override
public void onConsoleMessage(String message, int lineNumber, String sourceID)
Log.d("WebView", "Line: " + lineNumber + "," + message);
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage)
return false;
);
myWebView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(HomePage.this, "WebView Example", "Loading...");
myWebView.setWebViewClient(new WebViewClient()
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;
public void onPageFinished(WebView view, String url)
if (progressBar.isShowing())
progressBar.dismiss();
if (url.equals("https://www.codecell.in/jkdapp") == true)
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String username = preferences.getString(KEY_USERNAME, "");
String password = preferences.getString(KEY_PASSWORD, "");
if (username == null || password == null)
return;
view.loadUrl("javascript:fillvalues(" + username + "," + password + ");");
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(HomePage.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.show();
);
myWebView.addJavascriptInterface(new JavascriptInterface(), "Android");
myWebView.loadUrl("https://www.codecell.in/jkdapp");
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
myWebView.reload();
);
private class JavascriptInterface
public void saveValues(String name, String pass)
if (name == null || pass == null)
return;
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(KEY_USERNAME, name);
editor.putString(KEY_PASSWORD, pass);
editor.apply();
【问题讨论】:
您的具体问题是什么?我没听懂。 我只需要在登录页面输入的用户名出现在webview中 ***.com/questions/40889152/…。请检查此链接。您可以在 android 和 html 中创建方法进行通信。 【参考方案1】:public class StoreManager
private SharedPreferences.Editor editor;
private Context _context;
public SharedPreferences pref;
private int PRIVATE_MODE = 0;
private static final String PREF_NAME = "_store";
public StoreManager(Context _context)
this._context = _context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
public void setToken(String Token)
editor.putString("Token", Token);
System.out.println("token saved" + " " + Token);
editor.commit();
public String getToken()
return pref.getString("Token", "");
我像这样将我的数据保存为共享首选项中的字符串,当您登录时从那里获取数据并调用共享首选项的 setToken
StoreManager storeManager = new StoreManager(getApplicationContext());
storeManager.setToken("Your Username here");
你可以通过简单的调用以同样的方式获取数据
StoreManager storeManager = new StoreManager(getApplicationContext());
storeManager.getToken();
【讨论】:
以上是关于如何将登录详细信息存储在 Android 的本地存储中的主要内容,如果未能解决你的问题,请参考以下文章
不使用观察者无法在本地存储中存储用户详细信息(Firebase和Polymer身份验证)