Ich versuche SharedPreferences zu verwenden, um mein Konto für meine Apps zu registrieren, aber es gibt immer einen anderen Wert als das, was ich bei saveSipAccount
beim ersten Mal gespeichert habe, wenn ich versuche SharedPreferences zu bekommen getSipAccount
. Es funktioniert nur, wenn ich meine Apps neu starte. Ich bin Neuling für Android und SIP also bitte helfen Sie mir, vielen Dank!Ich kann keinen echten Wert erhalten, wenn ich SharedPreferences verwende
Dies ist mein Code
private static String PREFERENCE_NAME = "voip_demo_pref";
private static String SIP_DOMAIN_KEY = "sip_domain";
private static String SIP_PROXY_KEY = "proxy";
private static String SIP_USER_KEY = "user";
private static String SIP_PASSWORD_KEY = "password";
public void saveSipAccount(Context context, String domain, String proxy, String user, String password) {
SipAccount account = new SipAccount(domain, proxy, user, password);
saveSipAccount(context, account);
}
public void saveSipAccount(Context context, SipAccount account) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor e = pref.edit();
e.putString(SIP_DOMAIN_KEY, account.getDomain());
e.putString(SIP_PROXY_KEY, account.getProxy());
e.putString(SIP_USER_KEY, account.getUser());
e.putString(SIP_PASSWORD_KEY, encryptString(account.getPassword()));
e.apply();
}
public SipAccount getSipAccount(Context context) {
SipAccount account = new SipAccount();
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, context.MODE_PRIVATE);
account.setDomain(pref.getString(SIP_DOMAIN_KEY, ""));
account.setProxy(pref.getString(SIP_PROXY_KEY, ""));
account.setUser(pref.getString(SIP_USER_KEY, ""));
String password = pref.getString(SIP_PASSWORD_KEY, "");
if (!TextUtils.isEmpty(password)) {
password = decryptString(password);
}
account.setPassword(password);
return account;
}
und dies ist der Code, wenn ich saveSipAccount
String domain = String.valueOf(sipDomainView.getText());
String proxy = String.valueOf(sipProxyView.getText());
String user = String.valueOf(sipUserView.getText());
String password = String.valueOf(sipPasswordView.getText());
sipManager.saveSipAccount(MyApplication.getInstance().getApplicationContext(), domain, proxy, user, password);
Log.d(TAG, "saved");
try {
service.changeAccount();
}
nennen und das ist getSipAccount
public boolean changeAccount() {
sipManager = SipManager.newInstance();
SipAccount profile = sipManager.getSipAccount(MyApplication.getInstance().getApplicationContext());
AccountConfig config = sipManager.getAccountConfig(app, profile.getDomain(), profile.getProxy()
, profile.getUser(), profile.getPassword());
try {
MyApp.ep.libRegisterThread(Thread.currentThread().getName());
} catch (Exception e) {
Log.e(TAG, "libRegisterThread error.", e);
}
changeAccountStatus("processing");
try {
account.modify(config);
} catch (Exception e) {
Log.e(TAG, "MyAccount.modify error.", e);
changeAccountStatus("Fail registration");
return false;
}
return true;
}
können Sie diese Tasten mit der Frage 'SIP_DOMAIN_KEY' hinzufügen,' SIP_PROXY_KEY', 'SIP_PROXY_KEY',' SIP_PROXY_KEY', –
"** in ersten Mal, wenn ich versuche, SharedPreferences bei getSipAccount ** zu erhalten" hinzufügen Sie Code, wenn Sie speichere mit ** saveSipAccount ** und wenn du anrufst ** getSipAccount ** –
Bitte poste die ganze Klasse, wo du diese Methoden verwendest. – Hetfieldan24