我正计划实施复活节彩蛋,以通过alertdialog
更改 API ,但是目前,在更改url端点并提交后,它将保存在共享首选项中,但是接下来当我保存它时,它确实将其保存在共享的首选项中,但未反映在应用程序中。如何使它每次更改时都按预期适用于我的apiendpoint:
Alertdialog
,它会在5次点击时触发:
myimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long time= System.currentTimeMillis();
//if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
if (startMillis==0 || (time-startMillis> 3000) ) {
startMillis=time;
count=1;
}
//it is not the first, and it has been less than 3 seconds since the first
else{ // time-startMillis< 3000
count++;
}
if (count==5) {
final AlertDialog dialogBuilder = new AlertDialog.Builder(LoginActivity.this).create();
LayoutInflater inflater = LoginActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.edt_comment);
Button submitButton = (Button) dialogView.findViewById(R.id.buttonSubmit);
Button cancelButton = (Button) dialogView.findViewById(R.id.buttonCancel);
Button resetButton = (Button) dialogView.findViewById(R.id.buttonReset);
editText.setTextColor(getResources().getColor(R.color.black,null));
if(SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) != null) {
editText.setText( SharedPreferencesHelper.getSomeStringValue(getApplicationContext()));
}
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialogBuilder.dismiss();
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//save the endpoint value in shared preferences
SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),editText.getText().toString());
dialogBuilder.dismiss();
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),BuildConfig.BASE_URL);
dialogBuilder.dismiss();
}
});
dialogBuilder.setView(dialogView);
dialogBuilder.setCanceledOnTouchOutside(false);
dialogBuilder.show();
}
}
});
这是我的 sharedpreferenceshelper :
public class SharedPreferencesHelper {
private static final String APP_SETTINGS = "APP_SETTINGS";
// properties
private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
// other properties...
private SharedPreferencesHelper() {}
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
}
public static String getSomeStringValue(Context context) {
return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
}
public static void setSomeStringValue(Context context, String newValue) {
final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(SOME_STRING_VALUE , newValue);
editor.apply();
}
}
最后,这是我的Apiendpoint class
:
final class ApiEndPoint {
private static String NEW_URL = SharedPreferencesHelper.getSomeStringValue(MyApp.getAppContext());
static final String ENDPOINT_SERVER_LOGIN = NEW_URL
+ "/service-myverification-link/v1/link/verify";
private ApiEndPoint() {
// This class is not publicly instantiable
}
}
对于上面的代码,我想知道如何使 NEW_URL 链接更具动态性,好像它坚持第一次保存的值,此后不会更改。由于:
,我需要将此链接保持静态 @Override
public Single<LoginResponse> doServerLoginApiCall(LoginRequest request) {
return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_SERVER_LOGIN)
.doNotCacheResponse()
.addBodyParameter(request)
.build()
.getObjectSingle(LoginResponse.class);
}
您知道如何解决此问题,以便每次在 NEW_URL 中使用新值而不是第一次存储的值时都更新该链接吗?
答案 0 :(得分:0)
您将需要使用onSharedPreferenceChangeListener
SharedPreferencesHelper.getSharedPreferences(context).registerOnSharedPreferenceChangeListener(
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("SOME_STRING_VALUE")){
NEW_URL = prefs.getString("SOME_STRING_VALUE" , null);
}
}
});
答案 1 :(得分:0)
为什么每次都不发送Url作为参数:
@Override
public Single<LoginResponse> doServerLoginApiCall(LoginRequest request, String url) {
return Rx2AndroidNetworking.post(url)
.doNotCacheResponse()
.addBodyParameter(request)
.build()
.getObjectSingle(LoginResponse.class);
}
方法调用:
doServerLoginApiCall(request, SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) + ENDPOINT);
答案 2 :(得分:-2)
更改
final class ApiEndPoint {
private static String NEW_URL = SharedPreferencesHelper.getSomeStringValue(MyApp.getAppContext());
static final String ENDPOINT_SERVER_LOGIN = NEW_URL
+ "/service-myverification-link/v1/link/verify";
private ApiEndPoint() {
// This class is not publicly instantiable
}
}
到
final class ApiEndPoint {
public static String newURL(Context context) {
return SharedPreferencesHelper.getSomeStringValue(context) + "/service-myverification-link/v1/link/verify";
}
private ApiEndPoint() {
// This class is not publicly instantiable
}
}
并使用ApiEndpoint.newURL(this);