Java 无法删除在其他活动中设置的共享首选项
我想删除在LoginActivity上设置的authToken,但无法从作为MainActivity一部分的LogoutFragment中删除。想知道我哪里会出错。我看到了一些关于这方面的线索,但似乎没有一条有效。在LoginActivity中,我能够轻松清除共享pref 谢谢Java 无法删除在其他活动中设置的共享首选项,java,android,android-studio,sharedpreferences,Java,Android,Android Studio,Sharedpreferences,我想删除在LoginActivity上设置的authToken,但无法从作为MainActivity一部分的LogoutFragment中删除。想知道我哪里会出错。我看到了一些关于这方面的线索,但似乎没有一条有效。在LoginActivity中,我能够轻松清除共享pref 谢谢 package com.mpl.mpl.ui.logout; import android.content.Context; import android.content.Intent; import android.
package com.mpl.mpl.ui.logout;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.mpl.mpl.LoginActivity;
import com.mpl.mpl.databinding.FragmentLogoutBinding;
import com.mpl.mpl.ui.logout.LogoutViewModel;
public class LogoutFragment extends Fragment {
private LogoutViewModel logoutViewModel;
private FragmentLogoutBinding binding;
private WebView webView;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
logoutViewModel =
new ViewModelProvider(this).get(LogoutViewModel.class);
binding = FragmentLogoutBinding.inflate(inflater, container, false);
View root = binding.getRoot();
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(getActivity());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
Intent intent = new Intent(getActivity(), LoginActivity.class);
SharedPreferences preferences = getContext().getSharedPreferences("authToken", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
startActivity(intent);
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
登录片段:
package com.mpl.mpl.ui.login;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.mpl.mpl.MainActivity;
import com.mpl.mpl.R;
import com.mpl.mpl.databinding.FragmentLoginBinding;
import com.mpl.mpl.restClient.MplRestClient;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginFragment extends Fragment implements View.OnClickListener {
private LoginViewModel loginViewModel;
private FragmentLoginBinding binding;
private WebView webView;
private Button button;
private Button forgottenPasswordBtn;
private Button registerBtn;
private EditText email;
private EditText password;
private EditText errorMessageField;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
loginViewModel =
new ViewModelProvider(this).get(LoginViewModel.class);
binding = FragmentLoginBinding.inflate(inflater, container, false);
View root = binding.getRoot();
webView = (WebView) root.findViewById(R.id.loginWebView);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
SharedPreferences pref;
pref = getActivity().getPreferences(Context.MODE_PRIVATE);
String token = pref.getString("authToken", null);
if (token != null) {
webView.loadUrl("javascript:testFunction('" + token + "');");
}
try {
getLoginStatus();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
webView.loadUrl("hidden");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new IJavascriptHandler(), "cpjs");
password = (EditText) root.findViewById(R.id.passwordField);
email = (EditText) root.findViewById(R.id.emailField);
errorMessageField = (EditText) root.findViewById(R.id.statusMessage);
button = (Button) root.findViewById(R.id.buttonTest);
button.setOnClickListener(this);
registerBtn = root.findViewById(R.id.registerBtn);
registerBtn.setOnClickListener(this);
forgottenPasswordBtn = (Button) root.findViewById(R.id.forgottenPasswordBtn);
forgottenPasswordBtn.setOnClickListener(this);
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onClick(View v) {
//do what you want to do when button is clicked
switch (v.getId()) {
case R.id.buttonTest:
try {
getLoginStatus();
} catch (JSONException e) {
e.printStackTrace();
}
break;
case R.id.forgottenPasswordBtn:
Uri uri = Uri.parse("hidden"); // missing 'http://' will cause crashed
Intent forgottenIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(forgottenIntent);
break;
case R.id.registerBtn:
NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment_login_content_main);
navController.navigate(R.id.nav_register);
break;
}
}
public void getLoginStatus() throws JSONException {
MplRestClient.post("ajax/logintest.php?eml=" + email.getText().toString() + "&pwdr=" + password.getText().toString(), null, new JsonHttpResponseHandler() {
@SuppressLint("SetTextI18n")
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
try {
String message = response.getString("status");
switch (message) {
case "verify":
errorMessageField.setText("Please Verify Your Email");
errorMessageField.setVisibility(View.VISIBLE);
break;
case "success":
String token = response.getString("token");
errorMessageField.setText("");
errorMessageField.setVisibility(View.GONE);
webView.loadUrl("javascript:testFunction('" + token + "');");
break;
default:
errorMessageField.setText("Please Check Your Details");
errorMessageField.setVisibility(View.VISIBLE);
break;
}
} catch (Exception e) {
Toast.makeText(getActivity(), "fail",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
}
});
}
final class IJavascriptHandler {
IJavascriptHandler() {
}
@JavascriptInterface
public void sendToAndroid(String text) {
if (text.length() > 11) {
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("authToken", text);
edt.commit();
pref = getActivity().getPreferences(Context.MODE_PRIVATE);
String id = pref.getString("authToken", null);
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
}
}
}
LoginFragment是LoginActivity的一部分,共享首选项在IJavascriptHandler部分中设置 以下是设置首选项的方式:
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("authToken", text);
edt.commit();
这就是您清理的方式:
SharedPreferences preferences = getContext().getSharedPreferences("authToken", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
看到区别了吗
使用getActivity().getPreferences(Context.MODE\u PRIVATE)
设置首选项时,您将其设置为以活动命名的活动特定文件:
当您使用getContext().getSharedReferences(“authToken”,Context.MODE\u PRIVATE)
获取首选项时,您正在尝试访问一组名为“authToken”的应用程序范围内的首选项:
所以你在一个地方写作,在另一个地方阅读,所以当然不会匹配
解决方案
在获取和设置首选项的方式上保持一致。最简单的方法是使用应用程序范围的首选项
设置:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity())
SharedPreferences.Editor edt = pref.edit();
edt.putString("authToken", text);
edt.apply();
澄清:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext())
SharedPreferences.Editor editor = preferences.edit();
editor.remove("authToken")
editor.apply();
你的问题 以下是设置首选项的方式:
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("authToken", text);
edt.commit();
这就是您清理的方式:
SharedPreferences preferences = getContext().getSharedPreferences("authToken", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
看到区别了吗
使用getActivity().getPreferences(Context.MODE\u PRIVATE)
设置首选项时,您将其设置为以活动命名的活动特定文件:
当您使用getContext().getSharedReferences(“authToken”,Context.MODE\u PRIVATE)
获取首选项时,您正在尝试访问一组名为“authToken”的应用程序范围内的首选项:
所以你在一个地方写作,在另一个地方阅读,所以当然不会匹配
解决方案
在获取和设置首选项的方式上保持一致。最简单的方法是使用应用程序范围的首选项
设置:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity())
SharedPreferences.Editor edt = pref.edit();
edt.putString("authToken", text);
edt.apply();
澄清:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext())
SharedPreferences.Editor editor = preferences.edit();
editor.remove("authToken")
editor.apply();
显示您的登录活动-它的工作位置。很可能您正在以两种不同的方式访问它。@dominicoder我在设置authToken的位置添加了LoginFragment,因为您使用的是MODE_PRIVATE,所以在使用getPreferences()时也要发送“token”,如前所述。也检查是的-就像我想的那样。查看我的答案。向您展示登录活动-它在哪里工作。很可能您正在以两种不同的方式访问它。@dominicoder我在设置authToken的位置添加了LoginFragment,因为您使用的是MODE_PRIVATE,所以在使用getPreferences()时也要发送“token”,如前所述。也检查是的-就像我想的那样。看到我的答案了。这个解释非常好,非常感谢你的帮助:)这个解释非常好,非常感谢你的帮助:)